Data Structures and Algorithms (BCO 002A) UNIT-5
Data Structures and Algorithms (BCO 002A) UNIT-5
Data Structures and Algorithms (BCO 002A) UNIT-5
(BCO 002A)
UNIT-5
GRAPH
• Cycle: A path in which the first and last vertices are same
and no repeated edges or vertices.
• A graph is said to be acyclic if it contains no cycles.
• A directed graph that is acyclic is called Directed Acyclic
Graph (DAG).
• Connected Graph: A graph is called connected graph, if
and only if there is a simple path between any two vertices
of a graph. A connected graph without any cycle is a tree.
TERMINOLOGY OF GRAPH
from v0.
• They repeat this process until they have finally visited all
vertices.
GRAPH TRAVERSAL ALGORITHMS
Example:
Consider the graph G given on next slide.
The adjacency list of G is also given.
Assume that G represents the daily flights between different
cities and we want to fly from city A to H with minimum stops.
That is, find the minimum path P from A to H given that every
edge has length = 1.
BREADTH FIRST SEARCH
ALGORITHM
Adjacency Lists
A: B, C, D
B: E
C: B, G
D: C, G
E: C, F
F: C, H
G: F, H, I
H: E, I
I: F
BREADTH FIRST SEARCH
ALGORITHM
Example:
Consider the graph G given on next slide.
The adjacency list of G is also given.
Suppose we want to print all nodes that can be reached from the
node H (including H itself).
One alternative is to use a Depth- First Search of G starting at node H.
DEPTH FIRST SEARCH
ALGORITHM
Adjacency Lists
A: B, C, D
B: E
C: B, G
D: C, G
E: C, F
F: C, H
G: F, H, I
H: E, I
I: F
DEPTH FIRST SEARCH
ALGORITHM
b) Pop and Print the top element of the STACK, that is, H. Push all the
neighbors of H on to the stack that are in the ready state. The STACK
now becomes:
STACK: E, I
DEPTH FIRST SEARCH
ALGORITHM
c) Pop and Print the top element of the STACK, that is, I. Push all the
neighbors of I on to the stack that are in the ready state. The STACK
now becomes:
PRINT: I STACK: E, F
d) Pop and Print the top element of the STACK, that is, F. Push all the
neighbors of F on to the stack that are in the ready state. (Note F has
two neighbors C and H. but only C will be added as H is not in the
ready state). The STACK now becomes:
PRINT: F STACK: E, C
DEPTH FIRST SEARCH
ALGORITHM
e) Pop and Print the top element of the STACK, that is, C. Push all the
neighbors of C on to the stack that are in the ready state. The STACK
now becomes:
PRINT: C STACK: E, B, G
f) Pop and Print the top element of the STACK, that is, G. Push all the
neighbors of G on to the stack that are in the ready state. Since there
are no neighbors of G that are in the ready state no push operation is
performed. The STACK now becomes:
PRINT: G STACK: E, B
DEPTH FIRST SEARCH
ALGORITHM
g) Pop and Print the top element of the STACK, that is, B. Push all the
neighbors of B on to the stack that are in the ready state. Since there
are no neighbors of B that are in the ready state no Push operation is
performed. The STACK now becomes:
PRINT: B STACK: E
h) Pop and Print the top element of the STACK, that is, E. Push all the
neighbors of E on to the stack that are in the ready state. Since there
are no neighbors of E that are in the ready state no Push operation is
performed.
PRINT: E STACK: NULL
DEPTH FIRST SEARCH
ALGORITHM
i) Since the STACK is now empty, the depth-first search of G starting
at node H is complete and the nodes which were printed are- H,
I, F, C, G, B E.
SPANNING TREE
• This algorithm finds the shortest path to a vertex to the rest of the
vertices in a graph.
• First, it explores the shortest path from the source to a vertex nearest to
it, then to a second nearest, and so on.
• The one constraint for this algorithm is that all the edges must be non-
negative edges. Following are given:
G = (V, E) is a weighted connected graph,
W = weight matrix
S = set of visited vertices from source vertex to destination vertex,
initially it is empty.
s = source vertex, dist[v] = distance of vertex v from s
DIJKSTRA’S ALGORITHM
Algorithm:
1. Initialise d and pi,
2. Set S to empty,
3. While there are still vertices in V-S:
a. Sort the vertices in V-S according to the current best
estimate of their distance from the source,
b. Add u, the closest vertex in V-S, to S,
c. Relax all the vertices still in V-S connected to u.
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM
DIJKSTRA’S ALGORITHM