Ds- Unit 4- Problem on Graph
Ds- Unit 4- Problem on Graph
Step 2:
1 Delete '1' and print it
So 1'gets printed
Front
rear
in
vertex 1 and mark them as visited, insert those
vertices of
Step 3 : Find adjacent
Queue.
Visited
from
0 1 2 3 Increment front by 1delete '2'printed.
1 Queue and print it so '2' gets
1
Front Rear
Visited
Front Rear
visited. If it is
whether it is marked as
check
Step 6: Find adjacent to '3' i.e. 4
queue.
marked as visited do not insert in the
4 - 14
Algorithns
Siructuresand
Data
Step 1 : Start with vertex 1, print it so "1 gets printed. Mark 1 as visited.
Visited
1 1
2
3
4
Step 3:Find adjacent to "2' i.e. vertex 4 if it is not visited call DES4) ie. Awill
ushed on to the stack mark it as visited.
Visited Stack
00 4 -Top
2 1 After exiting the loop 4will be
popped print '4'
3 0
4 1
Step 4: Find adjacent to '4 i.e. vertex 3 if it is not visited call DFS(3) i..ee. 3il
shed onto the stack mark it
visited.
Visited Stack
|3 Top After exiting the loop 3 will be
2
popped print '3'
31
4
niversity Questions
Data Structures and Algorithms 4-30
Graphs
|4.6.4 Comparison between DFS and BFS
Sr. Depth first traversal Breadth first traversal
No.
1. This traversal is done with the help of stack This traversal is done with the help of
data structure. queue data structure.
2. It works using two ordering The first order is It works using one ordering. The order
the order in which the vertices are reached for in which the vertices are reached in the
the first time(i.e. the visited vertices are pushed same order they get removed from the
onto the stack) and the second order in which queue.
the vertices become dead end(the vertices are
popped off the stack)
3. The DFS sequence is composed of tree edges The DFS sequence is composed of tree
and back edges. edges and cross edges.
4. The efficiency of The adjacency matrix graph is The efficiency of The adjacency matrix
graph is O(V2)
5. The efficiency of the adjacency list graph is O
(|V|+|E|) The efficiency of the adjacency list
6.
graph is e(|V+|E)
Application : To check connectivity,
a graph and to find articulation pointacyclicity
of Applications: To check connectivity,
used.
DFS is acyclicity of a graph and to find
shortest path between two vertices BFS
is used. wwwwwwwwwwnos
Structuresand Algor1thms
Dala
1 2
b
Solution:
Step 1 Step 2 Step 3 Step 4 Step 5
a
a
3. 3
3
C 1 C 1 2
1 b -o d
b b o
6 6
3 4
Fig. 4.8.3
Solution :
Step 1
Step 2:
b
3
e
4
4
b
2
b Total weight = 15
2
d
Fig. 4.8.3(a)
Solution:
(1) (2)
1 1
2
8 1
V6 3 V
2 13
(3) (4)
12 8 1 8
12 V6 2
3 3 V2
V3
Graph G
V3 V5d
V V4
Minimum weight=21
Dijkstra'sshortest
path algorithm
2 shortest path algorithm suggests the shortest path from some source
Dijkstra's
The Some other destination node. The source node or the node from we start
nodetothe
distance is called the start node and the destination node is called the end
measuringthe we start finding the distance from the start node and find all the
algorithm
node.In this
tromn it to neighbouring nodes.
Among those which ever is the nearest node that
process of finding the nearest node is repeated till the end node.
paths
path isselected. This
Then
whichever is the path that path is called the shortest path.
choosing the shortest
Since in this algorithm all the paths are tried and then we are
them, this algorithm is solved by a greedy algorithm. One more thing is
Pathamong
doesn't
hat we are having all the vertices in the shortest path and therefore the graph
give the spanning tree.
example by this algorithm.
For clear understanding let us see stepwise solving of one
Example :
b 2 2
A
22 20
16 10
7
10
6
Fig. 4.9.2
already selected
P= Set which is for nodes which have
T= Remaining node
Step 1: V=a
P = {al, T = (b,c,d,e,f,z}
dist(b) = minfold dist(b),dist(a)+w(a,b)}
dist(b) = min{o,0+22)
dist(b) = 22
dist(c) = 16
dist(d) = 8 - minimum node
dist(e) = o
dist(f) = o
dist(z) =
Jo mininmum node is selected in P i.e. node d
Step 2: v= d Graçha
P= la,d) T = (bc,e,f,z)
dist(b) = min{old dist(b), dist(d)+w(b,d))
dist(b) = min{22,8+oo)
dist(b) = 22
dist(c) = min{16,8-+10) = 16
dist(e) = minlo,8too} = 8
dist(f) = minfo,8+6} = 14
dist(z) = minfo,8+oo} = oo
Step 3: V=f
P= (a,d,f} T = (b,c,e,z)
dist(b) = min(22,14+7} = 21
dist(c) = min{16,14+3) = 16
dist(e) = minfoo ,14 +oo =0
dist(z) = minfo, 14+9} = 23
Step 4: V=c
P= fa,d,f,c} T = (b,e,z)
dist(b) = min(21,16+20} = 21
dist(e) = minfo 16+4} = 20
dist(z) = min{23,16+10} = 23
Step 5: V= e
P= {a,d,f,c,e} T ={b,z}
dist(b) =min{21,20+2) = 21
dist(z) =min{23,20+4) = 23
Step 6: V =b
P= (a,d,f,c,e,b) T = (z)
dist(z) = min{23,21+2) = 23 of the
Hence the length
Now the target vertex for finding the shortest path is z.
shortest path from the vertex a to z is 23.
Fig. 4.9.3
Dala Structures and Algorithns 4- 42
EntorThe Soues1
Shortest path: 1 3 5
Exampl 4.4 Tind the shortest path from node 1 to 7 using shortest path
algorithm.
May-11, Marks 3
8 5
3 4
6
5
Fig. 4.9.4
Solution:
dist(5)
dist(6) = o
dist(7) =
T=2,5,6,7}
Sep3:P= 3, 4}
V=4
dist(2) = 6
min fo,5 + 6}
dist(5) = min{ dist(1,5),. dist(1, 4) +dist(4,5)}=
dist(5) = 11
= min fo, 5 +1}
dist(6) =min dist(1, 6), dist(1, 4) +dist(4, 6)}
=64-Select it.
dist(7) = oo
2 5
1
3
3
6
Fig. 4.9.5
Solution :
Step 1::P= (S), T= la, b, c, e, f, g, h, i, j, k, D}
= o
dist(S, a) = 3-Min dist(S, g) = o
dist(S, e)
dist(S, b) = 5 for knowledge
PUBLICATIONS- An up thrust
TECHNICAL
Data Structures and Algorithms 4-44
dist(S, c) = 7 dist(S, f) = co
Graphs
dist(S, h) = oo dist(S, k) = o
dist(S, i) = oo dist(S, D) =
dist(S, j) = oo
dist(j)
dist(k) = 16
dist(D)
Step 7: P= (S, a, g, b, f, c, i), T = (e, h, j, k, D}
V = i
dist(e) = 9 ’ Min
dist(h) = 9
dist(j)
dist(k) = 16
dist(D) = 9
Step 8: P= (S, a, 8, b, f, c, i, e), T =(h, j, k, D}
V= e
dist(k) = 16 ’ Min
dist(D) = 9
1. From a given graph find a vertex with no incoming edges. Delete it along with all
the edges outgoing from it. Ifthere are more than one such vertices then break the
tie randomly.
2. Note the vertices that are deleted.
3. All these recorded vertices give topologically sorted list.
Let us understand this algorithm with some examples -
Example 4.11.1 Sort the digraph for topological sort using source removal algorithm.
Fig. 4.11.1
Graphs
Delete
Delete
B
A Delete
D
B,A
delete delete
E
Sorted List: B.A,D
B,A,D,C B,A,D,C,E
Fig. 4.11.2
Hence the list after topological sorting will be B, A, D, C,
E.
Example 4.11.2 Sort the given digraph
using topological sort using source
removal algorithm.
B
Fig. 4.11.3
Solution: Let us start with the vertex with no
vertices Aand B. The tie can be incoming edge. There are two SlCn
broken arbitrarily. Let us first delete vertex B.
A
deiete
delete
B
A
B
delete
delete delete 8,A,C,D,E
C
D E
B,A
B,A,C
B,A,C,D
Fig. 4.11.4
Thus the topologically sorted list is B, A, C, D, E.
Structures and Algorithms
Dala.
4- 49
Graphs
nle 411.3 Apply source removal algorithm to solve topological sorting problem for the
followinggraph.
Fig. 4.11.5
Solution : We will find the node having no incoming edge. Such a vertex is d. Hence
delete it first along with its adjacent edges.
b
delete
delete
delete
a
d,a
delete
delete
b
d,a,c d,a,c,b
e e
delete
delete,
d,a,c,b,g d,a,c,b.g,f
Fig. 4.11.6