Analysis and Design of Algorithms
(ADA)
GTU # 3150703
Unit-6:
Exploring Graphs
Dr. Gopi Sanghani
Computer Engineering Department
Darshan Institute of Engineering & Technology, Rajkot
gopi.sanghani@darshan.ac.in
9825621471
Outline
Looping
An introduction using Graphs
Undirected Graph
Directed Graph
Traversing Graphs
Depth First Search (DFS)
Breath First Search (BFS)
Topological sort
Connected components
Articulation point
Introduction to Graph
Graph - Definition
A graph consists of a non-empty set 𝑵 called the set of nodes (vertices) of the graph, a set 𝑨
called the set of edges that also represents a mapping from the set of edges 𝑨 to a set of pairs
of elements 𝑵.
edges
(or links)
D
A
E
C
B
F nodes
(or vertices)
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 4
Directed & Undirected Graph
Directed Graph: A graph in which every edge is directed from one node to another is
called a directed graph or digraph.
Undirected Graph: A graph in which every edge is undirected and no direction is
associated with them is called an undirected graph.
Directed Graph Undirected Graph
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 5
Traversing Graphs
Traversing Graph/Tree
Preorder
i. Visit the root.
ii. Traverse the left sub tree in preorder.
iii. Traverse the right sub tree in preorder.
In order
i. Traverse the left sub tree in in order.
ii. Visit the root.
iii. Traverse the right sub tree in in order.
Post order
i. Traverse the left sub tree in post order.
ii. Traverse the right sub tree in post order.
iii. Visit the root.
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 7
Depth-First Search / Traversal
1 Select any node as starting point mark that
node as visited
2 3 4 Select one of the unvisited adjacent of
current node.
Make it new starting point and mark it as
5 6 7 8 visited
If new node has no unvisited adjacent then
move to parent and make it starting point
Visited : 1 2 3 6 5 4 7 8
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 8
DFS – Procedure
Let be an undirected graph all of whose nodes we wish to visit.
It is somehow possible to mark a node to show it has already been visited.
To carry out a depth-first traversal of the graph, choose any node as the starting point.
Mark this node to show it has been visited.
If there is a node adjacent to that has not yet been visited, choose this node as a new starting
point and call the depth-first search procedure recursively.
When all the nodes adjacent to are marked, the search starting atis finished.
If there remain any nodes of that have not been visited, choose any one of them as a new
starting point, and call the procedure again.
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 9
Depth-First Search Algorithm
procedure dfsearch(G)
for each v Є N do
mark[v] ← not-visited
for each v Є N do
if mark[v] ≠ visited
then dfs(v)
procedure dfs(v)
{Node v has not previously been visited}
mark[v] ← visited
for each node w adjacent to v do
if mark[w] ≠ visited
then dfs(w)
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 10
Depth-First Search - Algorithm
1. dfs(1) Initial call
1
2. dfs(2) recursive call
3. dfs(3) recursive call
4. dfs(6) recursive call 2 3 4
5. dfs(5) recursive call;
progress is blocked
5 6 7 8
6. dfs(4) a neighbour of
node 1 that has not been visited
procedure dfs(v)
7. dfs(7) recursive call mark[v] ← visited
8. dfs(8) recursive call for each node w adjacent to v do
if mark[w] ≠ visited
9. There are no more nodes to visit then dfs(w)
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 11
Breadth First Search / Traversal
1 Select any node v ∈ N as starting point mark that
node as visited.
3 Enqueue visited v node into queue Q
2 4
Dequeue a node from the front of queue.
Find it’s all unvisited adjacent nodes, mark as
5 6 7 8 visited, enqueue into queue
1 2 3 4 5 6 7 8
Queue Q Visited : 1 2 3 4 5 6 7 8
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 12
Breadth First Search - Algorithm
procedure search(G) procedure bfs(v)
for each v Є N do Q ← empty-queue
mark[v] ← not visited mark[v] ← visited
for each v Є N do enqueue v into Q
if mark[v] ≠ visited while Q is not empty do
then bfs(v)
u ← first(Q)
dequeue u from Q
for each node w adjacent to u do
if mark[w] ≠ visited
then mark[w] ← visited
enqueue w into Q
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 13
Comparison of DFS and BFS
Depth First Search (DFS) Breath First Search (BFS)
DFS traverses according to tree depth. DFS BFS traverses according to tree level. BFS finds
reaches up to the bottom of a subtree, then the shortest path to the destination.
backtracks.
It uses a stack to keep track of the next location It uses a queue to keep track of the next location
to visit. to visit.
DFS requires less memory since only nodes on BFS guarantees that the space of possible moves
the current path are stored. is systematically examined; this search requires
considerably more memory resources.
Does not guarantee to find solution. If there is a solution, BFS is guaranteed to find
Backtracking is required if wrong path is it.
selected.
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 14
Comparison of DFS and BFS
Depth First Search Breath First Search
If the selected path does not reach to the solution BFS will not get trapped exploring an infinite
node, DFS gets stuck or trapped into an infinite loops.
loops.
The Time complexity of both BFS and DFS will be O(V + E),
where V is the number of vertices, and E is the number of
Edges.
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 15
Topological Sorting
A topological sort or topological ordering of a directed acyclic graph is a linear ordering of
its vertices such that for every directed edge from vertex to vertex , the vertex comes before
the vertex in the ordering.
Topological Sorting for a graph is not possible if the graph is not a DAG.
In DFS, we print a vertex and then recursively call DFS for its adjacent vertices. In
topological sorting, we need to print a vertex before its adjacent vertices.
Few important applications of topological sort are-
Scheduling jobs from the given dependencies among jobs
Instruction Scheduling
Determining the order of compilation tasks to perform in makefiles
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 16
Topological Sorting – Example 1
B C Identify nodes having in degree ‘0’
Select a node and delete it with its edges then add
A F node to output
D E
Output:
A B C D E F
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 17
Topological Sorting – Example 2
5 4
2 0 1
Output:
4 5 0 2 3 1
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 18
Connected Components
A connected component (or just component) of an undirected graph is a subgraph in which
any two vertices are connected to each other by paths.
1 0 3
4
2
There are two connected components in the above undirected graph. 0 1 2 and 3 4
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 19
Connected Components A directed graph is strongly connected if
there is a directed path from any vertex to
every other vertex.
This is same as connectivity in an undirected
graph, the only difference is strong
connectivity applies to directed graphs and
there should be directed paths instead of just
paths.
Similar to connected components, a directed
graph can be broken down into Strongly
Connected Components.
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 20
Articulation Point
An articulation point in a connected graph is a vertex, that if is deleted (and edges through
it) then disconnects the graph.
It represent vulnerabilities in a connected network, single points whose failure would split
network into two or more disconnected components.
For a disconnected undirected graph, an articulation point is a vertex removing which will
increases number of connected components.
1 5
1 2 3
3 7
2 6
4 5
4
Articulation Points:
2,3 Articulation Points: 3
Dr. Gopi Sanghani #3150703 (ADA) Unit 6 –Exploring Graphs 21
Thank You!