0% found this document useful (0 votes)
61 views

Unit Vii Graphs Graphs: Comparison Between Graph and Tree

The document defines and describes graphs and their representations. It discusses: - What a graph is composed of (vertices and edges) - Types of graphs (directed, undirected) - Representations of graphs in memory (adjacency matrix, linked list) - Graph algorithms for creating and inserting vertices into graphs - Additional graph concepts like paths, cycles, connectivity, and degrees
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Unit Vii Graphs Graphs: Comparison Between Graph and Tree

The document defines and describes graphs and their representations. It discusses: - What a graph is composed of (vertices and edges) - Types of graphs (directed, undirected) - Representations of graphs in memory (adjacency matrix, linked list) - Graph algorithms for creating and inserting vertices into graphs - Additional graph concepts like paths, cycles, connectivity, and degrees
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 21

UNIT VII GRAPHS

Graphs

Graph: - A graph is a collection of two sets V and E. Where V is a finite non empty set of
vertices and E is a finite non-empty set of edges.
A
Vertices are nothing but the nodes in the graph.
Two adjacent vertices are joined by edges B C D
Any graph is denoted as G = {V, E}

For Example: E F

G = {{A, B, C, D, E, F}, {E1, E2, E3, E4, E5, E6}}

Comparison between Graph and Tree: -

Graph Tree
Non-linear data structure. Non-linear data structure.
Collection of vertices and edges. Collection of vertices and edges.
Each node can have any number of edges. In binary trees every node can have at the most
2 child nodes.
There is no unique node like trees. There is a unique node called root node.
A cycle can be formed. There will not be any cycle.
Applications: used for finding shortest path in Applications: used for expression trees, and
networking graph. decision tree.

Types of Graph:- Basically graphs are of two types: -

Directed graph: - In the directed graph the directions are shown on the edges. As shown in
figure, the edges between the vertices are ordered. In this type of the graph, the edge between
the vertices A and B is set of (A, B). The vertex A is called head and the vertex B is called
tail. We can say edge is set of (A, B) and not of (B, A).
Undirected graph: - In an undirected graph, the edges are not ordered. In the fig, the edge
between A and B is set of (A, B) and (B, A).

A A
DS NEC, CSE Dept. Page 1
B C D B C D

E F E F
UNIT VII GRAPHS

(a) Directed Graph (b) Undirected Graph

Complete Graph: - If an undirected graph of n vertices consists of n (n-1)/2 number of edges


then it is called as complete graph.

A path is a sequence of vertices in which each vertex is adjacent to the next one. In directed
graph {A, B, C, F} is one path and {A, B, E, F} is another.

Two vertices in a graph are said to be adjacent vertices (or) neighbors if there is a path of length
1 connecting them. In directed graph: B is adjacent to A, where as E is not adjacent to D. In
undirected graph: E and F are adjacent, but D and F are not.

A cycle is a path consisting of at least three vertices that starts and ends with the same vertex.

A loop is a special case of a cycle in which a single arc begins and ends with the same vertex.

A B

(c) Graph with loop


C D
Two vertices are said to be connected if there is a path between them. A graph is said to be
connected if, ignoring direction, there is a path from any vertex to any other vertex.

A directed graph is strongly connected if there is a path from each vertex to every other vertex
in the digraph.

A directed graph is weakly connected if at least two vertices are not connected.

A graph is a disjoint graph if it is not connected.

DS NEC, CSE Dept. Page 2


B E F G

C D H I
UNIT VII GRAPHS

(d) Weakly Connected

B E F G

(e) Strongly Connected


C D H I

B E F G

(f) Disjoint Graph


C D H I

The degree of a vertex is the number of lines incident to it. In fig (b), the degree of vertex B is 3
and the degree of vertex E is 2. The out degree of a vertex in a digraph is the number of arcs
leaving the vertex. The in degree of a vertex is the number of arcs entering the vertex. In fig (d)
for the vertex B the in degree is 1 and the out degree is 2.

Representation of Graphs in Memory:

Adjacency Matrix Representation: Matrix representation is the most commonly useful way
of representing any graph. This representation uses a square matrix of order nXn, where n is the
number of vertices in the graph.
DS NEC, CSE Dept. Page 3
UNIT VII GRAPHS

Entries in the matrix can be divided as follows:


Aij = 1 if there is an edge from vi to vj.
Aij = 0 other wise.

The matrix is also known as adjacency matrix, because an entry shows the information whether
the two vertices are adjacent (or) not. It is also termed as bit matrix (or) boolean matrix as its
entries are either 1 (or) 0.

V1

V V B E

2 3

V V V V C D
4 5 6 7

Fig (g) G1 fig (h) G2

1234567 ABCD

1 0110000 A0 0 3 0

2 1001100 B5 0 1 7

3 1000011 C2 0 0 4

4 0100000 D0 6 8 0

5 0100000 Matrix Representation of G2

6 0010000

7 0010000 Matrix Representation of G1

The disadvantage of adjacency matrix representation is that it always requires a nXn matrix with
n vertices, regardless of the number of edges. The adjacency matrix for an undirected graph is
symmetric. The adjacency matrix for a directed graph need not be symmetric.

Linked List Representation:

This representation of graph is used to save space.


DS NEC, CSE Dept. Page 4
NODE_LABEL ADJ_LIST WEIGHT NODE_LABEL ADJ_LIST

V1 V2 V3 \0
V327654 V321 \0 V64 V75 \0
UNIT VII GRAPHS

\0
Fig (i): Representation of Graph G1.

struct head struct node


{ {
int data; int ver;
struct head *down; struct node *link;
struct node *next; }
}

This is purely the adjacency list graph. The down pointer helps us to go to each next node in the
graph whereas the next node is for going to adjacent node of each of the head node. All the
advantages of linked list can be obtained in this type of graph.

Graph Algorithms: - Data structure is used for storing the graph. The most flexible
structure is the adjacency list implemented as a singly linked list.

The graph data structure is shown below:

GraphHead GraphVertex GraphArc


Count nextVertex destination
First data nextArc
End GraphHead inDegree End GraphArc

DS NEC, CSE Dept. Page 5


UNIT VII GRAPHS

outDegree
End GraphVertex

GraphHead structure stores information about each vertex. Graph data is shown in GraphVertex
structure. GraphArc structure stores data about an arc i.e. path between two vertices.

Create Graph: This algorithm initializes the metadata elements for graph data structure:
Algorithm: CreateGraph
1: set count to 0
2: set first to NULL
3: return GraphHead
End CreateGraph

Insert Vertex: Insert vertex adds a disjoint, i.e. an unconnected, vertex to the graph. The arcs
(path) associated with the vertex must be inserted separately.
Algorithm: InsertVertex (graph, dataIn)
1: allocate memory for new vertex
2: store dataIn in new vertex
3: initialize necessary elements in new Node
4: increment graphCount
Now find insertion point
5: if (empty graph)
1: set graph first to new Node
6: else
1: search for insertion point
2: if (inserting before first vertex)
1: set graph first to new vertex
3: else
1: insert new vertex in sequence
7: end if
End InsertVertex
The algorithm is same as singly linked list insertion situation. After allocating memory for the
new vertex, we move in the data and set all of its necessary data values to NULL.

Delete Vertex: In this algorithm, the first thing to do is we need to search for the vertex to be
deleted is present in the graph or not. Once we have found it, we need to ensure that there are no
arcs leaving (or) entering the vertex. If there is an arc, we reject the deletion.

DS NEC, CSE Dept. Page 6


UNIT VII GRAPHS

This is basic singly linked list delete situation. The only complexity is that we cannot delete a
vertex if its degree is greater than 0.

Algorithm DeleteVertex (graph, key)


1: if (empty graph)
1: return -2
2: end if
Locate vertex to be deleted.
3: search for vertex to be deleted
4: if (not found)
1: return -2
5: end if
Found vertex to be deleted
6: if (vertex inDegree > 0 or outDegree > 0)
1: return -1
7: end if
8: delete vertex
9: decrement GraphCount
10: return 1
End DeleteVertex

Insert Arc: Once we have a vertex, we can connect it to other vertices. Insert Arc requires two
points in the graph: the source vertex and the destination vertex. Each vertex is identified by its
key value rather than by its physical address.

After allocating memory for the new arc, we locate the fromVertex and toVertex. This logic
involves simple linked list searches. If either search fails, we return the appropriate value, -2 for
fromVertex not found and -3 for toVertex not found. If we locate both vertices, we increase there
degree count and set the new arcs destination pointer to the destination vertex. Then, we can
insert the new arc into the adjacency list.

Algorithm: InsertArc (graph, fromKey, toKey)


1: allocate memory for new arc
Locate source vertex
2: search and set fromVertex
3: if (fromVertex not found)
1: return -2
4: end if
Now locate toVertex
5: search and set toVertex

DS NEC, CSE Dept. Page 7


UNIT VII GRAPHS

6: if (toVertex not found)


1: return -3
7: end if
From and to vertices located. Insert new arc
8: increment fromVertex outDegree
9: increment toVertex inDegree
10: set arc destination to toVertex
11: if (fromVertex arc list empty)
Inserting first arc
1: set fromVertex first arc to new arc
2: set new arc next arc to NULL
3: return 1
12: end if
Find insertion point in adjacency list (arc)
13: find insertion point in arc list
14: if (insert at beginning of arc list)
Insertion before first arc
1: set fromVertex first arc
15: else
1: insert into arc list
16: end if
17: return 1
End InsertArc

Delete Arc: The delete arc algorithm removes one arc from the adjacency list. To identify an arc,
we need two vertices. The vertices are identified by their key. The algorithm therefore first
searches the vertex list for the start vertex and then searches its adjacency list for the destination
vertex. After locating and deleting the arc, the degree in the from and to vertices is adjusted.
The algorithm processes as follows:
1. Locate the sourceVertex
2. Locate the toVertex
3. Delete the arc

The source vertex is located by searching the vertex list. Once we have located it, we search the
adjacency list for an arc that points to the destination vertex. Once we find the correct arc, we
adjust the degree fields in the from and to vertex entries and then delete the arc.
Algorithm: DeleteArc (graph, fromKey, toKey)
1: if (empty)
1: return -2
2: end if
DS NEC, CSE Dept. Page 8
UNIT VII GRAPHS

Locate source vertex


3: search and set fromVertex to vertex with key equal to fromKey
4: if (fromVertex not found)
1: return -2
5: end if
Locate destination vertex in adjacency list
6: if (fromVertex arc list NULL)
1: return -3
7: end if
8: search and find arc with key equal to toKey
9: if (toKey not found)
1: return -3
10: end if
fromVertex, toVertex and arc all located then Delete arc
11: set toVertex to arcDestination
12: delete arc
13: decrement fromVertex outDegree
14: decrement toVertex inDegree
15: return 1
End DeleteArc

Retrieve Vertex: Retrieve vertex return the data stored in a vertex. Given the key of the vertex,
then the date are placed in the output. This algorithm is a typical linked list search algorithm. If
the search is successful, the data in the vertex are placed in the output area specified in the
calling sequence and +1 is returned. If the list is NULL (or) if the data cannot be located, then
return -2.
Algorithm: RetrieveVertex (graph, key, dataOut)
1: if (empty graph)
1: return -2
2: end if
3: search for vertex
4: if (vertex found)
1: move location pointer to dataOut
2: return +1
5: else
1: return -2
6: end if
End RetrieveVertex

Spanning Tree: A spanning tree has a property that for any pair of vertices there exists
only one path between them, and the insertion of any edge to a spanning tree does not form a

DS NEC, CSE Dept. Page 9

F
UNIT VII GRAPHS

cycle. The spanning tree resulting from a call to depth first tree is known as depth first spanning
tree. The spanning tree resulting from a call to breadth first tree is known as a breadth first
spanning tree. The minimum spanning tree for a graph is the spanning tree of minimum cost for
that graph.

Algorithm:
1. Insert the first vertex into the tree.
2. From every vertex already in the tree, examine the total path length to all adjacent vertices
not in the tree. Select the edge with the minimum total path weight and insert it into the tree.
3. Repeat step 2 until all vertices are in the tree.

Prims algorithm:

In the prims algorithm, minimum spanning tree grows in successive stages. At each stage in the
algorithm, we see that whether any of vertices have already been included in the tree, prims
algorithm then finds a new vertex to add it to the tree by choosing the edge <V i, Vj>, the smallest
among all edges, where Vi is the tree and Vj is yet to be included in the tree. The algorithm starts
by selecting a vertex arbitrarily, and then at each stage, we add an edge to the tree.

DS NEC, CSE Dept. Page 10


UNIT VII GRAPHS

This algorithm can easily be implemented using adjacency matrix representation of a graph.
Suppose there is a nXn adjacency matrix for a given undirected weighted graph and the vertices
are labeled as V1, V2, V3, .., Vi, Vj, and Vn. Start from Vertex V1. Get its nearest neighbor, which is
the vertex that has the smallest entry in the row of V 1, Let it be Vj. Now consider Vj and V1 as one
sub-graph, i.e., a vertex other than V1 and Vj that has the smallest entry among all the entries in
the rows of V1 and Vj, let his new vertex be V. Therefore, the tree now grows with vertex V 1, Vj,
and V as one sub-graph. This process is repeated until all N vertices have been connected by n-1
edges.

Consider the following example to find minimum spanning tree: An undirected weighted graph
and its weighted adjacency matrix is shown in following figure:

V1 1 2 3 4 5 6 7
1 - 4 5 2 - - -
V2 V3
2 4 - - 1 2 - -
3 5 - - 8 - 1 -
V4 4 2 1 8 - 3 4 7
5 - 2 - 3 - - 9
V5 V6
6 - - 1 4 - - 6
V7 7 - - - 7 9 6 -

Consider vertex V1 and pick the smallest entry in row 1 which is 2 in column 4; this V 4 is the
nearest neighbor to V1, the closest neighbor of sub-graph V 1 and V4 is V2 that can be seen by
examining all entries in rows 1 and 4. The remaining four edges relate to the above procedure.

V1 V1 V1 V1

Initial Tree V2 V2

V4 V4 V4
stage 1 stage 2 stage 3
V5

V1 V1 V1

V2 V2 V3 V2 V3
DS NEC, CSE Dept. Page 11
V4 V4 V4

V5 V5 V6 V5 V6
UNIT VII GRAPHS

V6

V7
Stage 4 stage 5 Final stage

Applications of Graph: -

In computer networking such as Local Area Network (LAN), Wide Area Network
(WAN), internetworking.
In telephone cabling graph theory is efficiently used.
In job scheduling algorithms.
Since graph is nothing but the collection of nodes (vertices) and edges. One can find the best
suited distance between the two vertices is reduced then the cost of calling and the time traveling
through the nodes will get reduced.

Transitive Closure: The Transitive closure matrix, denoted as A+, of a directed graph G, is
a matrix such that

A+ [i][j] = 1; if there is a path of length > 0 from i to j;


A+ [i][j] = 0; Otherwise.

Graph
0 1 2 3 4

0 1 2 3 4
0. 0 1 0 0 0
1. 0 0 1 0 0
2. 0 0 0 1 0 Adjacency Matrix A
3. 0 0 0 0 1
4. 0 0 1 0 0

0 1 2 3 4
1. 0 1 1 1 1
2. 0 0 1 1 1
3. 0 0 1 1 1 Adjacency Matrix A+

DS NEC, CSE Dept. Page 12


UNIT VII GRAPHS

4. 0 0 1 1 1
5. 0 0 1 1 1

Dijkstras shortest path: - Dijkstras shortest path is used to find the shortest path
between two vertices in a network. For example, if the network represents the routes flown by an
airline, when we travel we want to find the least expensive route between home and our
destination. When the weights in the route graph are the flight fare, our minimum cost is the
shortest path between 2 nodes. Edsger dijkstra developed a classic algorithm for just this
problem. This algorithm is generally known simply as Dijkstras shortest path algorithm.

This algorithm can be understood with the help of an example.

Consider the digraph shown in figure:

V V The adjacency matrix shows the cost of edges for


the digraph. Also, the matrix on the right side indicates the
1 4

path between two vertices.

V V
2 3
1234
1. 75-- 11 12 - -
2. 7--2 21 - - 24
3. -3-- - 32 - -
4. 4-1- 41 - 43 -

Some of the values of adjacency matrix are infinity (-), which indicates that there is no direct
path between the vertices. The blank sign in right hand side matrix indicates that till now the path
is not determined.

The minimum cost between two vertices is determined by considering one vertex at one time.
Then the path from particular vertex is calculated. If the cost is found to be smaller then previous
cost then it is replaced with the new cost.

DS NEC, CSE Dept. Page 13


UNIT VII GRAPHS

Consider the vertex 1 in the given example, the adjacency matrix now holds the values as
follows:

1 2 3 4
1. 7 5 - - 11 12 - -
2. 7 12 - 2 21 212 - 24
3. - 3 - - - 32 - -
4. 4 9 1 - 41 412 43 -

The values shown in square are the values that are added. Here the path 412 is possible as vertex
1 is considered. But the path 124 is not possible because till now the vertex 2 is not considered.
So, via 2 we cannot travel to any other vertex.

Now the vertex 2 is considered, then the adjacency matrix is


1 2 3 4
1. 7 5 - 7 11 12 - 124
2. 7 12 - 2 21 212 - 24
3. 10 3 - 5 321 32 - 324
4. 4 9 1 11 41 412 43 4124

Now the vertex 3 is considered, the paths between the vertices 4-2 and 4-4 are changed because
these are the shortest as compared to earlier ones.
1 2 3 4
1. 7 5 - 7 11 12 - 124
2. 7 12 - 2 21 212 - 24
3. 10 3 - 5 321 32 - 324
4. 4 4 1 6 41 432 43 4324

Finally, the vertex 4 is considered, which results into the adjacency matrix that holds the shortest
path between any two vertices.
1 2 3 4
1. 7 5 8 7 11 12 1243 124
2. 6 6 3 2 21 2432 243 24
3. 9 3 6 5 3241 32 3243 324
4. 4 4 1 6 41 432 43 4324

Warshalls Algorithm: -

DS NEC, CSE Dept. Page 14


UNIT VII GRAPHS

This algorithm determines if there is a path from any vertex V i to another vertex Vj either directly
(or) through one (or) more intermediate vertices. With this algorithm, we can test the reachability
of all the pairs of vertices in a graph.

To compute the path matrix of a given graph, this algorithm is helpful. This algorithm treats the
entries in the adjacency matrix as bit entries and perform AND (^) and OR (v) boolean
operations on them.

Algorithm:
Input: A graph G whose pointer to its adjacency matrix is GPtr and vertices are labeled as 1, 2,
3, ., N; N being the number of vertices in the graph.
Output: The path matrix P
Data structure: Matrix representation of graph with pointer as GPtr.
Step 1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = GPtr [i][j]
2: end for
3: for k=1 to N do
1: for i=1 to N do
1: for j=1 to N do
1: P [i][j] = P [i][j] v (P [i][k] ^ P {k][j])
2: end for
2: end for
4: end for
5: return
6: stop

The functionality of this algorithm can be determined as follows:

In step1, the path matrix P is initialized with the adjacency matrix GPtr, which signifies the
initial path matrix. This matrix, P [i][j], maintains path which are direct i.e. if there is a direct
path from Vi to Vj.

In step3, the outer most loop will execute N times. For each k, it decides whether there is a path
from Vi to Vj (for all i, j = 1, 2, ., N) either directly (or) via k, i.e., from V i to Vk and then from
Vk to Vj. It therefore sets the P [i][j] entries to 0 (or) 1 accordingly.

DS NEC, CSE Dept. Page 15


UNIT VII GRAPHS

Graph Traversals: -
A graph can be traversed in two ways
Depth First Search (DFS)
Breadth First Search (BFS)
1. Both of these algorithms work on directed or undirected graphs.
2. Many advanced graph algorithms are based on the ideas of BFS or DFS.
3. Each of these algorithms traverses edges in the graph, discovering new vertices as it
proceeds.
4. The difference is in the order in which each algorithm discovers the edges.
V1
DS NEC, CSE Dept. Page 16
V V
2 3
V V V8 V
UNIT VII GRAPHS

Consider an example in Graph G which is represented by its adjacency lists.

V21 V2 V3 \0 V
7

V1 V4 V5 \0

(a) Graph G
V3 V1 V6 V7 \0

V4 V2 V8 \0

V5 V2 V8 \0

V6 V3 V8 \0

V7 V3 V8 \0

V8 \0 V4 V5 V6 V7 \0

(b) Adjacency list of Graph G

BFS:

BFS starts at vertex V and mark it as visited. It then visits each of the vertices on Vs adjacency
list. When we have visited all the vertices on Vs adjacency list, we visit all the unvisited vertices
that are adjacent to the first vertex on Vs adjacency list. To implement this scheme, as we visit
each vertex we place the vertex in a queue. When we have exhausted an adjacency list, we
remove a vertex from the queue and proceed by examining each of the vertices on its adjacency
list. Unvisited vertices are visited and then placed on the queue; visited vertices are ignored. The
search is finished when the queue is empty.

V1
Example: Graph G. Source vertex: V1

DS NEC, CSE Dept. Page 17


UNIT VII GRAPHS

Visit V1, mark it as visited and Enqueue to Queue. Queue after Enqueue
Dequeue V1 and call BFS on V1.
Find V1 adjacent unvisited node i.e. V2 Queue after Dequeue

Visit V2, mark it as visited and Enqueue to Queue. V2

Find V1 adjacent unvisited node i.e. V3. Queue after Enqueue


V2 V3
Visit V3, mark it as visited and Enqueue to Queue.
Since all adjacent nodes of V1 are visited Dequeue from Queue. Queue after Enqueue
Dequeue V2 and call BFS on V2. V3

Find V2 adjacent unvisited node i.e. V4. Queue after Dequeue

Visit V4, mark it as visited and Enqueue to Queue. V3 V4

Find V2 adjacent unvisited node i.e. V5. Queue after Enqueue

Visit V5, mark it as visited and Enqueue to Queue. V3 V4 V5

Since all adjacent nodes of V2 are visited Dequeue from Queue. Queue after Enqueue
Dequeue V3 and call BFS on V3. V4 V5

Find V3 adjacent unvisited node i.e. V6. Queue after Dequeue

Visit V6, mark it as visited and Enqueue to Queue. V4 V5 V6

Find V3 adjacent unvisited node i.e. V7. Queue after Enqueue

Visit V7, mark it as visited and Enqueue to Queue. V4 V5 V6 V7

Since all adjacent nodes of V3 are visited Dequeue from Queue. Queue after Enqueue
V5 V6 V7
Dequeue V4 and call BFS on V4.
Find V4 adjacent unvisited node i.e. V8. Queue after Dequeue
V5 V6 V7 V8
Visit V8, mark it as visited and Enqueue to Queue.
Since all adjacent nodes of V4 are visited Dequeue from Queue. Queue after Enqueue.
Dequeue V5 and call BFS on V5. V6 V7 V8

Since all adjacent nodes of V5 are visited Dequeue from Queue. Queue after Dequeue
V7 V8
Dequeue V6 and call BFS on V6.
Since all adjacent nodes of V6 are visited Dequeue from Queue. Queue after Dequeue
V8
Dequeue V7 and call BFS on V7.
Since all adjacent nodes of V7 are visited Dequeue from Queue. Queue after Dequeue

DS NEC, CSE Dept. Page 18


UNIT VII GRAPHS

Dequeue V8 and call BFS on V8.


Since all adjacent nodes of V8 are visited Dequeue from Queue. Queue after Dequeue
Since Queue is Empty end of BFS on Graph G. Queue is Empty.

If a BFS is initiated from vertex V1, then the vertices of G are visited in the order V 1, V2, V3, V4,
V5, V6, V7, and V8.

DFS:

DFS is similar to a preorder traversal in trees. The DFS begins by visiting the start vertex V. Next
we select an unvisited vertex, W, from Vs adjacency list and carry out a DFS on W. We preserve
our current position in Vs adjacency list by placing it on a stack. Eventually our search reaches a
vertex, U, which has no unvisited vertices on its adjacency list. At this point, we remove a vertex
from the stack and continue processing its adjacency list. Previously visited vertices are
discarded; unvisited vertices are placed on the stack. The traversal terminates when the stack is
empty.

V1
Example: Graph G. Source Vertex V1.
Call DFS on V1. stack
Push V1 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V1) i.e. V2.
Call DFS on V2.
V1 V2
Push V2 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V2) i.e. V4. stack
Call DFS on V4.
Push V4 to stack & output, Mark as visited. V1 V2 V4

Find Adjacent unvisited node of stack top (V4) i.e. V8. stack
Call DFS on V8.
V1 V2 V4 V8
Push V8 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V8) i.e. V5. stack
Call DFS on V5.
V1 V2 V4 V8 V5

DS NEC, CSE Dept. Page 19


UNIT VII GRAPHS

Push V5 to stack & output, Mark as visited.


Find Adjacent unvisited node of stack top (V5) but all nodes are visited. stack
So pop the node form Stack (V5). V1 V2 V4 V8

Call DFS on stack top (V8). stack


Find Adjacent unvisited node of stack top (V8) i.e. V6.
Call DFS on V6.
V1 V2 V4 V8 V6
Push V6 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V6) i.e. V3. stack
Call DFS on V3.
Push V3 to stack & output, Mark as visited. V1 V2 V4 V8 V6 V3

Find Adjacent unvisited node of stack top (V3) i.e. V7. stack
Call DFS on V7.
V1 V2 V4 V8 V6 V3 V7
Push V7 to stack & output, Mark as visited.
Find Adjacent unvisited node of stack top (V7) but all nodes are visited. stack
V1 V2 V4 V8 V6 V3

So pop the node form Stack (V7)


Call DFS on stack top (V3).
Find Adjacent unvisited node of stack top (V3) but all nodes are visited.
V1 V2 V4 V8 V6

So pop the node form Stack (V3)


Call DFS on stack top (V6). stack
Find Adjacent unvisited node of stack top (V6) but all nodes are visited.
V1 V2 V4 V8

So pop the node form Stack (V6)


Call DFS on stack top (V8). stack
Find Adjacent unvisited node of stack top (V8) but all nodes are visited.
So pop the node form Stack (V8) V1 V2 V4

Call DFS on stack top (V4). stack


Find Adjacent unvisited node of stack top (V4) but all nodes are visited.

DS NEC, CSE Dept. Page 20


UNIT VII GRAPHS

So pop the node form Stack (V4) V1 V2

Call DFS on stack top (V2). stack


Find Adjacent unvisited node of stack top (V2) but all nodes are visited.
V1

So pop the node form Stack (V2)


Call DFS on stack top (V1). stack
Find Adjacent unvisited node of stack top (V1) but all nodes are visited.
So pop the node form Stack (V1)

DFS on stack top. stack


Stack is Empty: End of DFS on Graph G.

If a DFS is initiated from vertex V1, then the vertices of G are visited in the order V 1, V2, V4, V8,
V5, V6, V3 and V7.

DS NEC, CSE Dept. Page 21

You might also like