2. Introduction to Graphs
Another non linear data structure.
In tree structure, there is a hierarchical relationship between
parent and children, that is, one parent and many children.
On the other hand, in graph, the relationship is from many
parents to many children.
Definition-A graph G = (V,E) is composed of:
V: set of vertices(or nodes)
E: set of edges connecting the vertices in V
• An edge e = (u,v) is a pair of vertices
• Example:
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)}
S. Durga Devi ,CSE,CBIT
4. Graph terminology
• Digraph- A digraph is also called a directed graph. It is a graph G, such
that, G=<V,E>, where V is the set of all vertices and E is the set of ordered
pair of elements from V.
• Edges have directions
v= A,B,C,D
E=((A,B),(A,C),(A,D),(D,B))
A B
C
D
A B
C
D
These are directed graphs
G directed if edges ordered pairs (u,v)
u v
G undirected-
if edges unordered pairs {u,v}
S. Durga Devi ,CSE,CBIT
5. Adjacent vertices
If two nodes are connected by an edge, they are neighbors (and the nodes are adjacentto each other).
The size of a graph is the number of nodes in it
The empty graph has size zero (no nodes).
Loop-An edge that connects the vertex with itself
Degree of a Vertex- the number of edges connected to that vertex
• Fordirected graph,
– the in-degree of a vertex v is the number of edges incident on to the vertex.
– the out-degree of a vertex v is the number of edges emanating from that vertex.
Examples
3
2
3
3
0
1 33
0
1 2
3 4 5 6
2
3 3
1 1 1 1
directed graph
in-degree
out-degree
0
1
2
in:1, out: 1
in: 1, out: 2
in: 1, out: 0
S. Durga Devi ,CSE,CBIT
6. path: sequence of vertices v1,v2,. . .vk such that consecutive vertices vi and vi+1 are adjacent.
a
d e
c
b a b
c
d e
a b e d c
simple path: no vertex is repeated
a b
c
d e
b e c
Cycle - some path with distinct vertices except that the last
vertex is the same as the first vertex
a c d a
a b
c
d e
Parallel edges- if there are one or
More than one edges between
the same pair of vertices.
S. Durga Devi ,CSE,CBIT
7. A graph without cycles is called acyclicgraph.
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 every node in a graph is connected to
every othernode.
Two vertices are said to be connected if there is a path from vi to vj or vj to
vi
connected graph: any two vertices are connected by some path. if there is a
path from vi to vj and vj to vi.
A directed graph is said to be strongly connected if for every pair of distinct
vertices vi,vj in G, there is a path from vi to vj and also from vj to vi.
Weakly connected graph- if a graph is not strongly connected but it is
connected
S. Durga Devi
,CSE,CBIT
10. 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
S. Durga Devi
,CSE,CBIT
11. More definitions : Loop
An edge that connects the vertex with itself
A B
C
D
Or an edge whose starting and end vertices are same
S. Durga Devi ,CSE,CBIT
12. Even More Terminology
connected not connected
•connected graph: any two vertices are connected by some path
At least two vertices
are not connected in a
graph is called
disconnected graph.
S. Durga Devi ,CSE,CBIT
13. 0 0
1 2 3
1 2 0
1 2
3(i) (ii) (iii) (iv)
(a) Some of the subgraph of G1
0 0
1
0
1
2
0
1
2
(i) (ii) (iii) (iv)
(b) Some of the subgraph of G3
0
1 2
3
G1
0
1
2
G3
Subgraphs Examples
S. Durga Devi ,CSE,CBIT
14. Simple graph
• Simple graph- A graph (digraph) if it does
not have any loops or parallel edges (edge
from v1 to v2 and v2 to v1) is called
simple graph.
S. Durga Devi ,CSE,CBIT
15. Representation of a Graph
There are two ways of representinga graph in memory:
SequentialRepresentation by means of Adjacency Matrix.
Linked Representation by means of Linked List.
S. Durga Devi ,CSE,CBIT
16. 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 containsentries of only 0 and 1
Since it stores the information of whether the adjacency vertices are present
or not.
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
S. Durga Devi ,CSE,CBIT
17. Linked List Representation
A
B
C
D
N E
B C D NULL
A B E NULLD
A C D NULL
A B E NULLC
C D NULL
A
B C
D E
It saves the memory.
The number of lists dependson the number of vertices in the graph.
The header node in each list maintains a list of all adjacent vertices of a
node.
S. Durga Devi ,CSE,CBIT
20. Comparison among various representations
• Adjacency matrix representation always requires n x n matrix
with n vertices, regardless of the number of edges. But from
the manipulation point of view, this representation is the best.
• Insertion, deletion and searching are concerned, in some cases,
linked list representation has an advantage, but overall
performance is consider, matrix representation of graph is
more powerful than all
S. Durga Devi ,CSE,CBIT
21. Graph Traversal Techniques
There are two standardgraph traversaltechniques:
Depth-FirstSearch (DFS)
Breadth-FirstSearch (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
visitof vertices.
The order of visiting nodes depends on the node we have selected first
and hence the order of visitingnodes may not be unique.
S. Durga Devi ,CSE,CBIT
22. Depth-First Search
DFS is similarto the in order traversal of a tree
Stack is used to implement the Depth- First-Search.
DFS follows the followingrules:
1. Select an unvisitednode X, visitit, and treat as the current
node
2. Find an unvisitedadjacentof the current node as Y, visitit,
and make it the new current node;
3. Repeat step 1 and 2 till there is a ‘dead end’. dead end means
a vertex which do not have adjacent nodes or no more nodes
can be visited.
4.aftercoming to a dead end we backtrackingalong to its parent
to see if it has another adjacent vertex other than Y and then
repeat the steps 1,2,3. until all vertices are visited.
S. Durga Devi ,CSE,CBIT
23. Depth-First Search
A stack can be used to maintain the track of all paths from any
vertex so as to help backtracking.
Initially starting vertex will be pushed onto the stack.
To visit a vertex, we are to pop a vertex from the stack, and
then push all the adjacent vertices onto it.
A list is maintained to store the vertices already visited.
When a vertex is popped, whether it is already visited or not
that can be known by searching the list.
If the vertex is already visited, we will simply ignore it and we
will pop the stack for the next vertex to be visited.
This procedure will continue till the stack not empty
S. Durga Devi ,CSE,CBIT
24. 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.
S. Durga Devi ,CSE,CBIT
25. The Depth First Search Tree Order : A, B, E, D, C
A
B
E
DC
A
B
E
DC
The Breadth First Search Tree Order : A, B, C, D, E
A
B
E
DC
Graph
S. Durga Devi ,CSE,CBIT
26. BFS Traversal Order
A B D E C G F H IA 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
S. Durga Devi ,CSE,CBIT
29. A spanningtree of a graph is just a sub graph that containsall the vertices and
is a tree( without any cycles in a graph)
A graph may have many spanning trees.
o
r
o
r
o
r
Some SpanningTrees from GraphAGraphA
Spanning Trees
S. Durga Devi ,CSE,CBIT
30. Minimum spanning tree
• Take an example that connect four cities in India like
vijayawada, hyderabad, banglore and mumbai by a graph
Hyderabad
Mumbai
Banglore vijayawada
270km
980km
580km
1000km
Find a short distance to cover all four cities
700km
650
S. Durga Devi ,CSE,CBIT
31. 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
WeightedGraph Minimum Spanning Tree
Algorithms to find Minimum SpanningTrees are:
Kruskal‘s Algorithm
Prim‘s Algorithm
S. Durga Devi ,CSE,CBIT
32. 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.
S. Durga Devi ,CSE,CBIT
35. Sort the edges by increasing edge weight
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
36. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
37. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
38. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Accepting edge (E,G) would create a cycle
S. Durga Devi ,CSE,CBIT
39. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
40. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
41. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
42. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
43. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
44. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
45. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
46. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G 3
2
4
6
3
4
3
4
8
4
3
10 edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
S. Durga Devi ,CSE,CBIT
47. Select first |V|–1 edges which do not
generate a cycle
edge dv
(D,E) 1
(D,G) 2
(E,G) 3
(C,D) 3
(G,H) 3
(C,F) 3
(B,C) 4
5
1
A
H
B
F
E
D
C
G
2
3
3
3
edge dv
(B,E) 4
(B,F) 4
(B,H) 4
(A,H) 5
(D,F) 6
(A,B) 8
(A,F) 10
Done
Total Cost = dv = 21
4
} not
considere
d
S. Durga Devi ,CSE,CBIT
48. 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.
S. Durga Devi ,CSE,CBIT
49. Walk-Through
Initialize array
K dv pv
A F
B F
C F
D F
E F
F F
G F
H F
4
25
A
H
B
F
E
D
C
G 7
2
10
18
3
4
3
7
8
9
3
10
2
S. Durga Devi ,CSE,CBIT
65. 4
A
H
B
F
E
D
C
G
2
3
4
3
3
Cost of Minimum Spanning
Tree = dv = 21
K dv pv
A T 4 H
B T 4 C
C T 3 D
D T 0
E T 2 F
F T 3 C
G T 2 D
H T 3 G
2
Done
S. Durga Devi ,CSE,CBIT
66. C
FE
A B
D
3
2
1 2
2
C
FE
A B
D
5
64
3
4
2
1 2
3
2
Minimum Spanning Tree for the abovegraph 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
S. Durga Devi ,CSE,CBIT