Data Structures
Topic-Graphs
Graphs
Basic Terminology
Representation of Graphs
Graph Traversals
 DFS and BFS
Spanning Trees
Prims Algorithm
Kruskals Algorithm
Dijkstra Algorithm
Definition: A graph G is a pair, G = (V, E), where V is a finite nonempty
set of vertices and E is called the set of edges.
a b
c
d e
V= {a,b,c,d,e}
E=(a,b), (a,c), (a,d), (b,e), (c,d), (c,e), (d,e)}
Introduction to Graphs
Example:
 A directed graph or digraph is one in which the edges have a direction.
 An undirected graph is one in which the edges do not have a direction.
 The size of a graph is the number of nodes in it
 The empty graph has size zero (no nodes).
 If two nodes are connected by an edge, they are neighbors (and the nodes
are adjacent to each other).
 A path is a sequence of nodes such that each node (but the last) is the
predecessor of the next node in the list.
Example: If v1,v2,. . .,vk are vertices then vi and vi+1 should be consecutive.
Graph Terminology
 The degree of a node is the number of edges it has.
 For directed graphs:
 The in-degree of a node is the number of in-edges it has.
 The out-degree of a node is the number of out-edges it has.
 An undirected graph is connected if there is a path from every
node to every other node.
 A directed graph is strongly connected if there is a path from
every node to every other node.
 A simple path is a path in which all the vertices, except possibly the first
and last vertices, are distinct.
 A cycle in G is a simple path in which the first and last vertices are the
same.
 A graph without cycles is called acyclic graph.
 Sub graph is a graph with subset of vertices and edges of a graph.
 A graph is called a simple graph if it has no loops and no parallel edges.
 A graph is termed as weighted graph if all the edges in it are labeled with
some weights.
 A complete graph is a graph if there is a path from every node to every
other node.
0
1
2
G2 in:1, out: 1
in: 1, out: 2
in: 1, out: 0
G1 3
0
1 2
3
3
3
3
if edges ordered pairs (u,v)
 
u v
Directed
if edges unordered pairs {u,v}
 
u v
Un Directed
A
D
B C
Acyclic graph
A
C
B
D
Strongly Connected
A B
C D
E
Complete Graph
A
C
B
10
6
9
Weighted Graph
A
D
B C
Cyclic graph
0 0
1 2 3
1 2 0
1 2
3
(i) ii) (iii) (iv)
(a) Some of the sub graph of G1
0
1 2
3
G1
0 0
1
0
1
2
(i) (ii) (iii)
(b) Some of the sub graph of G2
0
1
2
G2
Representation of a Graph
There are two ways of representing a graph in memory:
 Sequential Representation by means of Adjacency Matrix.
 Linked Representation by means of Linked List.
Adjacency Matrix
A B C D E
A 0 1 1 1 0
B 1 0 1 1 0
C 1 1 0 1 1
D 1 1 1 0 1
E 0 0 1 1 0
 Adjacency Matrix is a bit matrix which contains entries of only 0 and 1
 The connected edge between two vertices is represented by 1 and absence
of edge is represented by 0.
 This representation uses a square matrix of order n x n, where n is the
number of vertices in the graph.
 Adjacency matrix of an undirected graph is symmetric.
A
B C
D E
Linked List Representation
A
B
C
D
N E
B C D NULL
A B E NULL
D
A C D NULL
A B E NULL
C
C D NULL
A
B C
D E
 It saves the memory.
 The number of lists depends on the number of vertices in the graph.
 The header node in each list maintains a list of all adjacent vertices of a
node .
Undirected Graph Adjacency List Adjacency Matrix
Directed Graph Adjacency List Adjacency Matrix
Graph Traversal Techniques
There are two standard graph traversal techniques:
 Depth-First Search (DFS)
 Breadth-First Search (BFS)
 Traversing a graph means visiting all the vertices in the graph exactly once.
 DFS and BFS traversals result an acyclic graph.
 DFS and BFS traversal on the same graph do not give the same order of visit
of vertices.
Depth First Traversal:
The depth first traversal is similar to the in-order traversal of a binary tree.
An initial or source vertex is identified to start traversing, then from that vertex
any one vertex which is adjacent to the current vertex is traversed.
To implement the depth first search algorithm, we use a stack.
DFS follows the following rules:
1. Select an unvisited node x, visit it, and treat as the current node
2. Find an unvisited neighbor of the current node, visit it, and make it the
new current node;
3. If the current node has no unvisited neighbors, backtrack to the its
parent, and make that parent the new current node.
4. Repeat steps 3 and 4 until no more nodes can be visited.
5. If there are still unvisited nodes, repeat from step 1.
Breadth First Traversal:
The breadth first traversal is similar to the pre-order traversal of a binary tree.
The breadth first traversal of a graph is similar to traversing a binary tree
level by level (the nodes at each level are visited from left to right).
All the nodes at any level, i, are visited before visiting the nodes at level i + 1.
To implement the breadth first search algorithm, we use a queue.
BFS follows the following rules:
1. Select an unvisited node x, visit it, have it be the root in a BFS tree being
formed. Its level is called the current level.
2. From each node x in the current level, visit all the unvisited neighbors of
x. The newly visited nodes from this level form a new level that becomes
the next current level.
3. Repeat step 2 until no more nodes can be visited.
4. If there are still unvisited nodes, repeat from Step 1.
The Depth First Search Tree Order : A, B, E, D, C
A
B
E
D
C
A
B
E
D
C
The Breadth First Search Tree Order : A, B, C, D, E
A
B
E
D
C
Graph
Example1:
BFS Traversal Order
A B D E C G F H I
A B C F E G D H I
DFS Traversal Order
A B C
D E F
G H I
A B C
D E F
G H I
Example2:
Given Graph
From vertex B either C or D to be explored.
From start vertex A explore edges.
Example3: Construct the DFS and BFS for the following graph.
Depth First Search:
Since C is dead end, backtrack
to B, from there explore D.
From D it is possible to explore A, but this
would form a cycle, so again backtrack to B,
from there backtrack to A, explore the path to F.
From F it is possible to traverse either A or c, but
both are discovered already. So F is also a dead end.
Note: From the above diagram it is possible to say that G and E are never
traversed.
The DFS
Tree Order : A, B, C, D, F
The BFS Tree Order :
A,B,F,C,D
Breadth First Search:
Given Graph.
Explore all paths from vertex B and F.
The dashed lines indicate, nodes are
previously discovered.
From D the explored vertex is A.
But A is already visited.
From the start vertex A, the explored
vertices are B and F.
Note: From the above diagram it is possible to say that G and E are never
traversed.
Applications of Graphs
 Electronic circuits
 Printed circuit board
 Integrated circuit
 Transportation networks
 Highway network
 Flight network
 Computer networks
 Local area network
 Internet
 Web
 Databases
 Entity-relationship diagram
The BFS Tree Order :
A,B,F,C,D
Breadth First Search:
Given Graph.
Explore all paths from vertex B and F.
The dashed lines indicate, nodes are
previously discovered.
From D the explored vertex is A.
But A is already visited.
From the start vertex A, the explored
vertices are B and F.
Note: From the above diagram it is possible to say that G and E are never
traversed.
A spanning tree of a graph is just a sub graph that contains all the vertices and
is a tree.
A graph may have many spanning trees.
o
r
o
r
o
r
Some Spanning Trees from Graph A
Graph A
Spanning Trees
Minimum Spanning Trees
The minimum spanning tree for a given graph is the spanning tree of
minimum cost for that graph.
5
7
2
1
3
4
2
1
3
Weighted Graph Minimum Spanning Tree
Algorithms to find Minimum Spanning Trees are:
 Kruskal‘s Algorithm
 Prim‘s Algorithm
Kruskal’s algorithm
Kruskal’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
1. Draw all the vertices of the graph.
2. Select the smallest edge from the graph and add it into the spanning tree
(initially it is empty).
3. Select the next smallest edge and add it into the spanning tree.
4. Repeat the 2 and 3 steps until that does not form any cycles.
C
F
E
A B
D
5
6
4
3
4
2
1 2
3
2
C
F
E
A B
D
3
2
1
2
2
Minimum Spanning Tree for the above graph is:
Prim’s algorithm
Prim’s algorithm finds the minimum cost spanning tree by selecting the
edges one by one as follows.
1. All vertices are marked as not visited
2. Any vertex v you like is chosen as starting vertex and is marked as visited
(define a cluster C).
3. The smallest- weighted edge e = (v,u), which connects one vertex v inside
the cluster C with another vertex u outside of C, is chosen and is added to
the MST.
4. The process is repeated until a spanning tree is formed.
C
F
E
A B
D
3
2
1 2
2
C
F
E
A B
D
5
6
4
3
4
2
1 2
3
2
Minimum Spanning Tree for the above graph is:
A B C D E F
A - 5 4 6 2 -
B 5 - - 2 - 3
C 4 - - - 3 -
D 6 2 - - 1 2
E 2 - 3 1 - 4
F - 3 - 2 4 -
Adjacency matrix
1
3
4
6
5
2
6
3
6
5 5
1
6
4 2
5
Exercise:
The Single-Source Shortest path
Problem ( SSSP)
• Given a positively weighted directed graph G with a source
vertex v, find the shortest paths from v to all other vertices
in the graph.
V1 V2
V3
V4
V5
V6
Ex :-
V1V3
v
V1V3V4
V1V3V4V2
V1V5
5) V1V3V4V6 28
Iteration S Dist[2
]
Dist[3] Dist[4] Dist[5] Dist[6]
Initial {1} 50 10 ∞ 45 ∞
1 2
3 4
5
6
20
10
50 10
15 3
30
35
45
20

15

1 { 1,3 } 50 10 25 45 ∞


2 { 1,3,4 } 45 10 25 45 28


3 { 1,3,4,6 } 45 10 25 45 28


4 { 1,3,4,5,6 } 45 10 25 45 28

Data Structures-Non Linear DataStructures-Graphs

Data Structures-Non Linear DataStructures-Graphs

  • 1.
  • 2.
    Graphs Basic Terminology Representation ofGraphs Graph Traversals  DFS and BFS Spanning Trees Prims Algorithm Kruskals Algorithm Dijkstra Algorithm
  • 3.
    Definition: A graphG is a pair, G = (V, E), where V is a finite nonempty set of vertices and E is called the set of edges. a b c d e V= {a,b,c,d,e} E=(a,b), (a,c), (a,d), (b,e), (c,d), (c,e), (d,e)} Introduction to Graphs Example:
  • 4.
     A directedgraph or digraph is one in which the edges have a direction.  An undirected graph is one in which the edges do not have a direction.  The size of a graph is the number of nodes in it  The empty graph has size zero (no nodes).  If two nodes are connected by an edge, they are neighbors (and the nodes are adjacent to each other).  A path is a sequence of nodes such that each node (but the last) is the predecessor of the next node in the list. Example: If v1,v2,. . .,vk are vertices then vi and vi+1 should be consecutive. Graph Terminology
  • 5.
     The degreeof a node is the number of edges it has.  For directed graphs:  The in-degree of a node is the number of in-edges it has.  The out-degree of a node is the number of out-edges it has.  An undirected graph is connected if there is a path from every node to every other node.  A directed graph is strongly connected if there is a path from every node to every other node.
  • 6.
     A simplepath is a path in which all the vertices, except possibly the first and last vertices, are distinct.  A cycle in G is a simple path in which the first and last vertices are the same.  A graph without cycles is called acyclic graph.  Sub graph is a graph with subset of vertices and edges of a graph.  A graph is called a simple graph if it has no loops and no parallel edges.  A graph is termed as weighted graph if all the edges in it are labeled with some weights.  A complete graph is a graph if there is a path from every node to every other node.
  • 7.
    0 1 2 G2 in:1, out:1 in: 1, out: 2 in: 1, out: 0 G1 3 0 1 2 3 3 3 3 if edges ordered pairs (u,v)   u v Directed if edges unordered pairs {u,v}   u v Un Directed
  • 8.
    A D B C Acyclic graph A C B D StronglyConnected A B C D E Complete Graph A C B 10 6 9 Weighted Graph A D B C Cyclic graph
  • 9.
    0 0 1 23 1 2 0 1 2 3 (i) ii) (iii) (iv) (a) Some of the sub graph of G1 0 1 2 3 G1 0 0 1 0 1 2 (i) (ii) (iii) (b) Some of the sub graph of G2 0 1 2 G2
  • 10.
    Representation of aGraph There are two ways of representing a graph in memory:  Sequential Representation by means of Adjacency Matrix.  Linked Representation by means of Linked List.
  • 11.
    Adjacency Matrix A BC D E A 0 1 1 1 0 B 1 0 1 1 0 C 1 1 0 1 1 D 1 1 1 0 1 E 0 0 1 1 0  Adjacency Matrix is a bit matrix which contains entries of only 0 and 1  The connected edge between two vertices is represented by 1 and absence of edge is represented by 0.  This representation uses a square matrix of order n x n, where n is the number of vertices in the graph.  Adjacency matrix of an undirected graph is symmetric. A B C D E
  • 12.
    Linked List Representation A B C D NE B C D NULL A B E NULL D A C D NULL A B E NULL C C D NULL A B C D E  It saves the memory.  The number of lists depends on the number of vertices in the graph.  The header node in each list maintains a list of all adjacent vertices of a node .
  • 13.
    Undirected Graph AdjacencyList Adjacency Matrix
  • 14.
    Directed Graph AdjacencyList Adjacency Matrix
  • 15.
    Graph Traversal Techniques Thereare two standard graph traversal techniques:  Depth-First Search (DFS)  Breadth-First Search (BFS)  Traversing a graph means visiting all the vertices in the graph exactly once.  DFS and BFS traversals result an acyclic graph.  DFS and BFS traversal on the same graph do not give the same order of visit of vertices.
  • 16.
    Depth First Traversal: Thedepth first traversal is similar to the in-order traversal of a binary tree. An initial or source vertex is identified to start traversing, then from that vertex any one vertex which is adjacent to the current vertex is traversed. To implement the depth first search algorithm, we use a stack. DFS follows the following rules: 1. Select an unvisited node x, visit it, and treat as the current node 2. Find an unvisited neighbor of the current node, visit it, and make it the new current node; 3. If the current node has no unvisited neighbors, backtrack to the its parent, and make that parent the new current node. 4. Repeat steps 3 and 4 until no more nodes can be visited. 5. If there are still unvisited nodes, repeat from step 1.
  • 17.
    Breadth First Traversal: Thebreadth first traversal is similar to the pre-order traversal of a binary tree. The breadth first traversal of a graph is similar to traversing a binary tree level by level (the nodes at each level are visited from left to right). All the nodes at any level, i, are visited before visiting the nodes at level i + 1. To implement the breadth first search algorithm, we use a queue. BFS follows the following rules: 1. Select an unvisited node x, visit it, have it be the root in a BFS tree being formed. Its level is called the current level. 2. From each node x in the current level, visit all the unvisited neighbors of x. The newly visited nodes from this level form a new level that becomes the next current level. 3. Repeat step 2 until no more nodes can be visited. 4. If there are still unvisited nodes, repeat from Step 1.
  • 18.
    The Depth FirstSearch Tree Order : A, B, E, D, C A B E D C A B E D C The Breadth First Search Tree Order : A, B, C, D, E A B E D C Graph Example1:
  • 19.
    BFS Traversal Order AB D E C G F H I A B C F E G D H I DFS Traversal Order A B C D E F G H I A B C D E F G H I Example2:
  • 20.
    Given Graph From vertexB either C or D to be explored. From start vertex A explore edges. Example3: Construct the DFS and BFS for the following graph. Depth First Search:
  • 21.
    Since C isdead end, backtrack to B, from there explore D. From D it is possible to explore A, but this would form a cycle, so again backtrack to B, from there backtrack to A, explore the path to F. From F it is possible to traverse either A or c, but both are discovered already. So F is also a dead end. Note: From the above diagram it is possible to say that G and E are never traversed. The DFS Tree Order : A, B, C, D, F
  • 22.
    The BFS TreeOrder : A,B,F,C,D Breadth First Search: Given Graph. Explore all paths from vertex B and F. The dashed lines indicate, nodes are previously discovered. From D the explored vertex is A. But A is already visited. From the start vertex A, the explored vertices are B and F. Note: From the above diagram it is possible to say that G and E are never traversed.
  • 23.
    Applications of Graphs Electronic circuits  Printed circuit board  Integrated circuit  Transportation networks  Highway network  Flight network  Computer networks  Local area network  Internet  Web  Databases  Entity-relationship diagram
  • 24.
    The BFS TreeOrder : A,B,F,C,D Breadth First Search: Given Graph. Explore all paths from vertex B and F. The dashed lines indicate, nodes are previously discovered. From D the explored vertex is A. But A is already visited. From the start vertex A, the explored vertices are B and F. Note: From the above diagram it is possible to say that G and E are never traversed.
  • 25.
    A spanning treeof a graph is just a sub graph that contains all the vertices and is a tree. A graph may have many spanning trees. o r o r o r Some Spanning Trees from Graph A Graph A Spanning Trees
  • 26.
    Minimum Spanning Trees Theminimum spanning tree for a given graph is the spanning tree of minimum cost for that graph. 5 7 2 1 3 4 2 1 3 Weighted Graph Minimum Spanning Tree Algorithms to find Minimum Spanning Trees are:  Kruskal‘s Algorithm  Prim‘s Algorithm
  • 27.
    Kruskal’s algorithm Kruskal’s algorithmfinds the minimum cost spanning tree by selecting the edges one by one as follows. 1. Draw all the vertices of the graph. 2. Select the smallest edge from the graph and add it into the spanning tree (initially it is empty). 3. Select the next smallest edge and add it into the spanning tree. 4. Repeat the 2 and 3 steps until that does not form any cycles.
  • 28.
    C F E A B D 5 6 4 3 4 2 1 2 3 2 C F E AB D 3 2 1 2 2 Minimum Spanning Tree for the above graph is:
  • 29.
    Prim’s algorithm Prim’s algorithmfinds the minimum cost spanning tree by selecting the edges one by one as follows. 1. All vertices are marked as not visited 2. Any vertex v you like is chosen as starting vertex and is marked as visited (define a cluster C). 3. The smallest- weighted edge e = (v,u), which connects one vertex v inside the cluster C with another vertex u outside of C, is chosen and is added to the MST. 4. The process is repeated until a spanning tree is formed.
  • 30.
    C F E A B D 3 2 1 2 2 C F E AB D 5 6 4 3 4 2 1 2 3 2 Minimum Spanning Tree for the above graph is: A B C D E F A - 5 4 6 2 - B 5 - - 2 - 3 C 4 - - - 3 - D 6 2 - - 1 2 E 2 - 3 1 - 4 F - 3 - 2 4 - Adjacency matrix
  • 31.
  • 32.
    The Single-Source Shortestpath Problem ( SSSP) • Given a positively weighted directed graph G with a source vertex v, find the shortest paths from v to all other vertices in the graph. V1 V2 V3 V4 V5 V6 Ex :- V1V3 v V1V3V4 V1V3V4V2 V1V5 5) V1V3V4V6 28
  • 33.
    Iteration S Dist[2 ] Dist[3]Dist[4] Dist[5] Dist[6] Initial {1} 50 10 ∞ 45 ∞ 1 2 3 4 5 6 20 10 50 10 15 3 30 35 45 20  15  1 { 1,3 } 50 10 25 45 ∞   2 { 1,3,4 } 45 10 25 45 28   3 { 1,3,4,6 } 45 10 25 45 28   4 { 1,3,4,5,6 } 45 10 25 45 28 