Data Structures: Mahesh Goyani
Data Structures: Mahesh Goyani
STRUCTURES
MAHESH GOYANI
MAHATMA GANDHI INSTITUTE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com
A Graph G consists of
1. Set of vertices V (Called node), (V = {v1, v2, v3, ……}) and
2. Set of Edged E (Called Arc) (i.e. E {e1, e2, e3, ……})
A graph can be described as G = (V, E), where V is a finite and non empty set of
vertices and E is a set of pairs of edges.
A D
Set of Vertices V = {A, B, C, D, E}
Set of Edges E = {(A,B), (A, C), (A, D), (C, D), (C, E) }
C
If the pair of node s that make up the arc are ordered pairs, the graph is called
directed graph (or Diagraph) {<V1, V2>}
A graph which contains order less pairs of arc is called undirected graph { (V1, V2) }
A graph is called mixed graph if it contains set of ordered and order less arcs.
v1 v2
e1
v1 v2 Isolated Vertices
Directed Graph
e1
v1 v2
e4 e3
e2 v5 e7
e5 e6
e1 v4 v3
v1 v2
e9 e8
v6
Undirected Graph
(C) GOYANI MAHESH Mixed graph 4
TERMINOLOGY
a1
a b
ISOMORPHIC
GRAPH d1
d c c1 b1
a a a b
b b
SUB e SPANING SUB
GRAPH GRAPH e
d
c d c d c
a d X
b c
Two vertices are said to be adjacent if they are joined by an edge (i.e. (a, b)).
The number of edges incident on a vertex is called degree
Indegree of a node is number of arcs that have head as incident on that vertex.
Outdegree of a vertex is number of arcs that have tail as incident on that vertex.
Degree of isolated vertex is ZERO. (e.g. Node X)
Node b has outdegree 1, indegree 2 and degree 3.
A path from mode to it self is called cycle (Node C & D)
If a graph contains cycle, than it is cyclic graph, other wise it is acyclic graph
A directed acyclic graph is called a DAG
N
47 55
B 39
K
27 16
C
Undirected graph is said to be Connected Graph if there exist a path from any
vertex to any other vertex. Other wise it is called Disconnected Graph.
a c a c
b d b d
a c
Undirected graph is said to be Fully Connected
Graph (strongly connected / complete graph) if there
exist a path from all vertex to all other vertex.
e10 e3
e8 e9
v8 v9 v4
e12
e7 e11
e4
v7 v6 e5 v5
e6
In a directed graph, a path is a sequence of edges such that the edges are
connected with each other.
(e1, e3, e4, e5, e12, e9, e11, e6, e7, e8, e11)
A path is said to be elementary path if it does not meet the same vertex twice.
(e1, e3, e4, e5, e6, e7, e8)
A path is said to be simple path if it does not meet the same edges twice.
(e1, e3, e4, e5, e6, e7, e8, e11, e12)
A Circuit is a path in which terminal vertex en coincides with the initial vertex e1.
Simple circuit
(e1, e3, e4, e5, e12, e9, e10)
Elementary circuit
(e1, e3, e4, e5, e6, e7, e8, e10)
(C) GOYANI MAHESH 9
GRAPH REPRESENTATION
j 1 2 3 4 5
i
1 0 1 1 0 0
1 2
2 0 0 0 0 1
3 0 0 0 1 0
3 4 5 4 1 0 0 0 0
5 0 0 0 0 1
Directed Graph
i j 1 2 3 4 5
1 0 1 1 1 0
1 2
2 1 0 0 0 1
3 1 0 0 1 0
4 1 0 1 0 0
3 4 5
5 0 1 0 0 1
Undirected Graph
j 1 2 3 4 5
5 i
1 2 1 -1 5 3 -1 -1
2 7 2 -1 -1 -1 -1 7
3
0 3 -1 -1 -1 4 -1
3 4 5
4 4 2 -1 -1 -1 -1
5 -1 -1 -1 -1 0
Weighted Graph
1 2 3
1 2 2 5
3 4
3 4 5
4 1
Directed Graph 5 5
1 2 5 3 3
5
1 2
2 5 7
2 7
3
3 4 4
0
3 4 5
4 4 1 2
Weighted Graph
5 5 0
Create ( )
Search ( )
Delete ( )
Traverse ( )
Front = -1
Rear = -1
Front = 0
B C
A
D E F
Rear = 0
G H
Front = 1
I A B C
Rear = 2
A B C D E
A Rear = 4
B C
Front = 3
D E F A B C D E F
Rear = 5
G H
Front = 4
I
A B C D E F G
Rear = 6
A B C D E F G
A
Rear = 6
B C
Front = 6
D E F
A B C D E F G H
Rear = 7
G H
I Front = 7
A B C D E F G H
Rear = 7
A B C D E F G H
A
Rear = 7
B C
D E F
G H
3. Push S to STACK
7. Exit.
A
D TOP G TOP
B TOP E E
B C
A TOP C C C
D E F
A B D G E F H C
G H
I
E TOP F TOP H TOP
C C C C TOP
If graph G is not connected graph than it can not have any spanning tree. In
this case, it will have spanning forest.
Suppose if Graph G has n vertices than MST will have (n-1) edges.
4 7
2
1 2
10 4
3 6
8 7 9 2 6
1
4
5 2
4 3
5
1
6
1 2
2
9 2
8 6
1 2
5
3
4
Set V = {V1, V2, V3, …… Vn} contains the vertices and the edges E = {e1, e2, …en} of
the graph G. We is the weight of an edge e, which contains the vertices V1 and V2. Q is
a set of vertices, which are not visited. M is the vertex in Q for which weight Wm is
minimum i.e. minimum cost edge. S is source vertex.
9. Exit
B 5 C B
V A B C D E F
C
2 3
3 F 2
W(V) 0
D
Q A B C D E F
3 1 E
E 4 D F
A
6 5 m=A
5 W (A, A) = 0
B C
2 3 Now Q = (B, C, D, E, F)
3 F 2 Two edges incident with m
3 1
E 4 D i.e. I = (B, C)
W(B) = min( w(B), W(A) + W(A,B))
= (-, 0 + 6) = 6
W(C) = min (w(C) , W(A) + W(A,C))
= (-, 0 + 5) = 5
A
B A
V A B C D E F
C A
W(V) 0 6 5
D
Q B C D E F
E
F
A
6 5 m = C (Because W(C) is minimum & is member of Q)
5 Now Q = (B, D, E, F)
B C
2 3 Two edges incident with m
3 F 2 i.e. I = (D, F)
3 1
E 4 D W(D) = min( w(D), W(C) + W(C,D))
= (-, 5 + 2) = 7
W(F) = min (w(F) , W(C) + W(C,F))
= (-, 5 + 3) = 8
A
B A
V A B C D E F
C A
W(V) 0 6 5 7 8
D C
Q B D E F
E
F C
A
6 5 m = B (Because W(B) is minimum and is member of Q)
5 Now Q = (D, E, F)
B C
2 3 Three edges incident with m (C, E, F) but C is not in Q
3 F 2 So, I = (E, F)
3 1
E 4 D W(E) = min( w(E), W(B) + W(B,E))
= (-, 6 + 3) = 9
W(F) = min (w(F) , W(B) + W(B,F))
= (-, 6 + 2) = 8
A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q D E F
E B
F C
A
6 5 m = D (Because W(D) is minimum and is member of Q)
5 Now Q = (E, F)
B C
2 3 One edges incident with m
3 F 2 So, I = (F)
3 1
E 4 D
W(F) = min (w(F) , W(D) + W(D,F))
= (-, 7 + 1) = 8
A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q E F
E B
F C
A
6 5 m = F (Because W(F) is minimum and is member of Q)
5 s=F
B C
2 3 Q =(F)
3 F 2 One edges incident with m
3 1
E 4 D So, I = (E)
A
B A
V A B C D E F
C A
W(V) 0 6 5 7 9 8
D C
Q E
E B
F C