Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF
Httpslms Ou Edu Vn241pluginfile Php217729mod - resourcecontent1DsA-04-Graph PDF
• If the graph is unweighted, the adjacency matrix may contain boolean values.
• We can use 2D array to represent a graph.
Graph implementation
• If the graph was directed, then the matrix would not necessarily be
symmetric
Sparse matrix
• Matrices where less than 5% of the entries are not the default value
(either infinity or 0, or perhaps some other default value) are said
to be sparse.
• Matrices where most entries (25% or more) are not the default
value are said to be dense
Class Assignment: please declare complete structures for adjacent list to store a graph.
Adjacency list for graph
https://www.geeksforgeeks.org/graph-and-its-
representations/
Traversal algorithms in graph
• Graph Traversal on the graph: Traversal means visiting all the nodes of
a graph. All vertices in a graph need to be visited exactly once
• Graph Traversal is also used to decide the order of vertices to be visited
in the search process.
• A graph traversal finds the edges to be used in the search process
without creating loops. This means that, with graph traversal, we can visit
all the vertices of the graph without getting into a looping path. There
are two graph traversal techniques
• DFS (Depth First Search)
• BFS (Breadth-First Search)
• BFS and DFS are bases for many graph related algorithms
BFS applications
1. Shortest Path and Minimum Spanning Tree for unweighted graph
2. Peer to Peer Networks: to find all neighbor nodes
3. Crawlers in Search Engines
4. Social Networking Websites: to find people within a given distance k from a person.
5. GPS Navigation systems: to find all neighboring locations
6. Cycle detection in undirected graph
7. Ford–Fulkerson algorithm: to find the maximum flow.
8. Path Finding: to find if there is a path between two vertices
9. To test if a graph is Bipartite.
Many algorithms like Prim’s Minimum Spanning Tree and Dijkstra’s Single Source Shortest Path
use structure similar to Breadth First Search
BFS Algorithm
• Breadth-first search (BFS) is an algorithm that is used to graph data
or searching tree or traversing structures.
• This algorithm selects a single node (initial or source point) in a
graph and then visits all the nodes adjacent to the selected node
• Once the algorithm visits and marks the starting node, then it
moves towards the nearest unvisited nodes and analyses them.
Once visited, all nodes are marked. These iterations continue until
all the nodes of the graph have been successfully visited and
marked
Why BFS Algorithm
• BFS is useful for analyzing the nodes in a graph and constructing
the shortest path of traversing through these
• BFS can traverse through a graph in the smallest number of
iterations
• The architecture of the BFS algorithm is simple and robust
• The result of the BFS algorithm holds a high level of accuracy in
comparison to other algorithms
• BFS iterations are seamless, and there is no possibility of this
algorithm getting caught up in an infinite loop problem
How BFS works
Start from vertex 0, putting it in the Visited list and
putting all its adjacent vertices in the stack
A B C E
BFS example
Pop B and push D Pop C, push F
A B A B C
C E D E D F
BFS example
Pop E and push G and H Pop D
A B C E A B C E D
D F G H F G H
BFS example
Pop F Pop G and push I
A B C E D F A B C E D F G
G H H I
BFS example
Pop H Pop I
A B C E D F G H A B C E D F G H I
I
Rules of BFS Algorithm
• A queue (FIFO-First in First Out) data structure is used by BFS.
• You mark any node in the graph as root and start traversing the data from it.
• BFS traverses all the nodes in the graph and keeps dropping them as
completed.
• BFS visits an adjacent unvisited node, marks it as done, and inserts it into a
queue.
• Removes the previous vertex from the queue in case no adjacent vertex is
found.
• BFS algorithm iterates until all the vertices in the graph are successfully
traversed and marked as completed.
• There are no loops caused by BFS during the traversing of data from any node
BFS Implemtation
1. Create a queue and #include <queue>
enqueue/insert source into it vector<bool> v; vector<vector<int> > g;
i. Mark source as visited queue<int> q; q.push(u); v[u] = true;
2. While queue is not empty, do while (!q.empty())
following int f = q.front(); q.pop();
i. Dequeue/Remove a vertex from v[f] = true;
queue. Let this be f. for (auto i = g[f].begin(); i != g[f].end(); i++)
ii. Mark that vertex as visited if (!v[*i]) {
iii. Enqueue/Insert all the unvisited q.push(*i);
neighbours of the vertex into the v[*i] = true;
queue.
}
DFS applications
• Detecting cycle in a graph
• Path Finding
• Topological Sorting: to scheduling jobs from given dependencies
among jobs. Topological sorting can be done using DFS algorithm
• To test if a graph is bipartite
• Finding Strongly Connected Components of a graph: If there is a
path from each vertex to every other vertex, that is strongly
connected.
Starts by putting it in the Visited list and putting
A A B A B C
B C E C E D E D F
DFS Iterative example
A B C E A B C E D A B C E D F
D F G H F G H G H
DFS Iterative example
A B C E D F G A B C E D F G H A B C E D F G H I
H I I
DFS Recursive example
A A B A B C
DFS Recursive example
A B C D A B C D E A B C D E G
DFS Recursive example
A B C D E G I A B C D E G I H A B C D E G I H F
DFS-Iterative & Recursive
• An iterative DFS may also be different from recursive DFS
A B C E D F G H I A B C D E G I H F
DFS Iteraative Implemtation
1. Create a stack and insert source into it. stack<int> stack; stack.push(v);
Declared visited vector vector <bool> visited(n, false);
https://www.techiedelight.com/depth-first-search/
https://www.tutorialcup.com/interview/graph/iterative-depth-first-traversal-of-graph.htm
Summary
• We can use adjacent matrix or linked list to represent a graph.
• Two most popular graph traversals
• Breath First Search.
• Depth First Search
• BFS uses queue and DFS uses stack to implement
• We can use recursive or iterative (associate with stack) approach to
implement DFS