01. Graphs
-
Upload
akif-vohra -
Category
Documents
-
view
224 -
download
0
Transcript of 01. Graphs
-
8/8/2019 01. Graphs
1/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 11
DATASTRUCTURES
MAHESH GOYANI
MAHATMA GANDHI INSTITUTE OF TECHNICAL EDUCATION & RESEARCH CENTER
-
8/8/2019 01. Graphs
2/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 22
GRAPHS
-
8/8/2019 01. Graphs
3/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 33
TERMINOLOGY
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 ofvertices and E is a set of pairs of edges.
Tree must be a graph, but graph need not be a tree
E
D
C
A
B
Set of Vertices V = {A, B, C, D, E}
Set of Edges E = {(A,B), (A, C), (A, D), (C, D), (C, E) }
-
8/8/2019 01. Graphs
4/34
-
8/8/2019 01. Graphs
5/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 55
a b
d c
a1
d1
c1 b1
ISOMORPHICGRAPH
a b
c d
e
a b
c d
SUBGRAPH
a b
cd
eSPANING SUB
GRAPH
TERMINOLOGY
-
8/8/2019 01. Graphs
6/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 66
a
b
d
c
Two vertices are said to be adjacentif 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
TERMINOLOGY
X
-
8/8/2019 01. Graphs
7/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 77
A Graph is said to be weighted graph (network) if every edge and / or vertices inthe graph is assigned with some weight or value.
A Weighted graph can be defined as G = (V, E, We, Wv) where,
V = Set of vertices
E = Set of Edges
We = Set of Weight of edges
Wv = Set of Weight of Vertices
N
C
KB
47
27
55
39
16
TERMINOLOGY
-
8/8/2019 01. Graphs
8/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 88
Undirected graph is said to be Connected Graph if there exist a path from anyvertex to any other vertex. Other wise it is called Disconnected Graph.
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.
A complete graph with n vertices will have n(n-1)/2edges.
a
b
c
d
a
b
c
d
a
b
c
d
TERMINOLOGY
-
8/8/2019 01. Graphs
9/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 99
v1 v2 v3
v5v6
v9 v4
v7
v8
e1 e2
e3
e9
e4
e12
e5e6
e11e7
e8
e10
In a directed graph, a path is a sequence of edges such that the edges areconnected 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 Circuitis 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)
TERMINOLOGY
-
8/8/2019 01. Graphs
10/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1010
GRAPH REPRESENTATION
1. Sequential representation (Using Array)2. Linked representation (Using Linked List)
1
3
2
4 5
100005
000014
010003
100002
001101
54321i
j
Directed Graph
-
8/8/2019 01. Graphs
11/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1111
1
3
2
4 5100105
001014
010013
100012
011101
54321
Undirected Graph
i j
ARRAY REPRESENTATION
-
8/8/2019 01. Graphs
12/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1212
0-1-1-1-15
-1-1-1-124
-14-1-1-137-1-1-1-12
-1-135-11
54321ij
1
3
2
4 5
3
4
2
5
7
0
Weighted Graph
ARRAY REPRESENTATION
-
8/8/2019 01. Graphs
13/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1313
LINKED LIST REPRESENTATION
1
3
2
4 5
3
4
2
5
7
0
1
3
2
4 5
1 2 3
2 5
3 4
4 1
5 5
1 2 5 3 3
2 5 7
3 4 4
4 1 2
5 5 0
Directed Graph
Weighted Graph
-
8/8/2019 01. Graphs
14/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1414
OPERATION ON GRAPH
Create ( )
Search ( ) Delete ( )
Traverse ( )
-
8/8/2019 01. Graphs
15/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1515
1. Input the Vertices & Edges of the Graph G =( V, E )
2. Input the source vertex and assign it to thevariable S
3. Push S to Queue
4. While Front
-
8/8/2019 01. Graphs
16/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1616
A
B C
D E F
G H
I
Front = -1
Rear = -1
A
Front = 0
Rear = 0
A B C
Front = 1
Rear = 2
BREADTH FIRST SEARCH (BFS)
-
8/8/2019 01. Graphs
17/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1717
A
B C
D E F
G H
I
A B C D E
Front = 2
Rear = 4
A B C D E F
Front = 3
Rear = 5
A B C D E F G
Front = 4
Rear = 6
BREADTH FIRST SEARCH (BFS)
-
8/8/2019 01. Graphs
18/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1818
A B C D E F G
Front = 5
Rear = 6
A B C D E F G H
Front = 6
Rear = 7
A B C D E F G H
Front = 7
Rear = 7
A
B C
D E F
G H
I
BREADTH FIRST SEARCH (BFS)
-
8/8/2019 01. Graphs
19/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 1919
A B C D E F G H
Front = 8
Rear = 7A
B C
D E F
G H
I
BREADTH FIRST SEARCH (BFS)
-
8/8/2019 01. Graphs
20/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2020
1. Input the Vertices & Edges of the Graph G =( V, E )
2. Input the source vertex and assign it to thevariable S
3. Push S to STACK
4. While TOP != -1, repeat step 5 & 6.
5. Pop front element and display it
6. Push the vertices, which are neighbor to justpopped element, if it is not in the STACK and
displayed
7. Exit.
DEPTH FIRST SEARCH (DFS)
-
8/8/2019 01. Graphs
21/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2121
DEPTH FIRST SEARCH (DFS)
A
B C
D E F
G H
I
A
B
C
D
E
C
G
E
C
E
C
F
C
H
C C
A B D G E F H C
TOP
TOP
TOP TOP
TOP TOP TOP
TOP
-
8/8/2019 01. Graphs
22/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2222
MINIMUM SPANNING TREE (MST)
A minimum spanning tree for a graph G = (V, E) is a sub graph G1 = (V1,E1) of G, contains all the vertices of G, and
The vertex set V1 is same as that at graph G
The Edge E1 is sub set of G
And there is no cycle.
If graph G is not connected graph than it can not have any spanning tree. Inthis case, it will have spanning forest.
Suppose if Graph G has n vertices than MST will have (n-1) edges.
A MST for a weighted graph is a spanning tree with minimum weight.
-
8/8/2019 01. Graphs
23/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2323
8
1
5
2
6
3
7
4
9
10
4 7
2
4
5
6
7
3
5
1
6
4
2
1
2
MINIMUM SPANNING TREE (MST)
-
8/8/2019 01. Graphs
24/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2424
8
1
5
2
6
3
7
4
9
4
2
5
3
1
2
1
2
MINIMUM SPANNING TREE (MST)
-
8/8/2019 01. Graphs
25/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2525
SHORTEST PATH
A Path from source vertex a to b is said to be shortest path if there is no otherpath from a to b with lower weights.
Dijkstars Algorithm is used to find shortest path.
Set V = {V1, V2, V3, Vn} contains the vertices and the edges E = {e1, e2, en} ofthe graph G. We is the weight of an edge e, which contains the vertices V1 and V2. Q isa set of vertices, which are not visited. M is the vertex in Q for which weight Wm isminimum i.e. minimum cost edge. S is source vertex.
-
8/8/2019 01. Graphs
26/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2626
DIJKSTRAS ALGPRITHM
1. Input the source vertex and assign it to S.
(a). Set W(s) = 0 and
(b). Set W(v) = ______ for all verttices V is not equal to S
2. Set Q = V which is set of vertices in the graph.
3. Suppose m be a vertices in Q for which W(m) is minimum.
4. Make the vertices m as visited and delete it from Q
5. Find the vertices I which are incident with m and member of Q
6. Update the weight of vertices I = {i1, i2, , ik} by
(a) W(i) = min [W(i1), W(m) + W(m,i1)]
7. If any changes is made in W(v), store the vertices to correspondingvertices I, using the array for tracing the sortest path
8. Repeat the process from strep 3 to 7 untill Q is empty
9. Exit
-
8/8/2019 01. Graphs
27/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2727
A
B C
E D
F
6 5
5
32 3
1
2
4
3
Source vertex = A
V = (A, B, C, D, E, F) = Q
V A B C D E F
W(V) 0
Q A B C D E F
A
B
C
D
E
F
-
8/8/2019 01. Graphs
28/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2828
ITERATION : 1
m = A
W (A, A) = 0Now Q = (B, C, D, E, F)
Two edges incident with m
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 C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5
Q B C D E F
A
B A
C A
D
E
F
-
8/8/2019 01. Graphs
29/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 2929
ITERATION : 2
m = C (Because W(C) is minimum & is member of Q)
Now Q = (B, D, E, F)Two edges incident with m
i.e. I = (D, F)
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 C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5 7 8
Q B D E F
A
B A
C A
D C
E
F C
-
8/8/2019 01. Graphs
30/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 3030
ITERATION : 3
m = B (Because W(B) is minimum and is member of Q)
Now Q = (D, E, F)Three edges incident with m (C, E, F) but C is not in Q
So, I = (E, F)
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 C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5 7 9 8
Q D E F
A
B A
C A
D C
E B
F C
-
8/8/2019 01. Graphs
31/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 3131
ITERATION : 4
m = D (Because W(D) is minimum and is member of Q)
Now Q = (E, F)One edges incident with m
So, I = (F)
W(F) = min (w(F) , W(D) + W(D,F))
= (-, 7 + 1) = 8
A
B C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5 7 9 8
Q E F
A
B A
C A
D C
E B
F C
-
8/8/2019 01. Graphs
32/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 3232
ITERATION : 5
m = F (Because W(F) is minimum and is member of Q)
s = FQ =(F)
One edges incident with m
So, I = (E)
W(E) = min (w(F) , W(F) + W(F, E))
= (9, 9 + 3) = 9
A
B C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5 7 9 8
Q E
A
B A
C A
D C
E B
F C
-
8/8/2019 01. Graphs
33/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 3333
Now E is the only chain, hence we stop the iteration andthe final table is as bellow
A
B C
E D
F
6 5
5
32 3
1
2
4
3
V A B C D E F
W(V) 0 6 5 7 9 8
A
B A
C A
D C
E B
F C
-
8/8/2019 01. Graphs
34/34
(C) GOYANI MAHESH(C) GOYANI MAHESH 3434
APPLICATION
Shortest Path (City Map)
Flow Function (Water Flow)
Scheduling (Cook)