Вы находитесь на странице: 1из 128

Graph Algorithms

1
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
A graph G = (V,E) is composed of:
V: Finite, non-empty set of vertices
E: set of edges connecting the vertices in V
= Subset of VxV
An edge e = (u,v) is a pair of vertices.

a b
V={a,b,c,d,e}
c
E={(a,b),(a,c),(a,d),(b,e),
(c,d),(c,e),(d,e)}
d e
2
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
An undirected graph is one in which the pair
of vertices in a edge is unordered, (v0, v1) =
(v1,v0)
A directed graph is one in which each edge is
a directed pair of vertices, <v0, v1> != <v1,v0>
If (v0, v1) is an edge in an undirected graph,
v0 and v1 are adjacent
The edge (v0, v1) is incident on vertices v0 and v1
If <v0, v1> is an edge in a directed graph
v0 is adjacent to v1, and v1 is adjacent from v0

3
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
The degree of a vertex is the number of edges
incident to that vertex
For directed graph,
the in-degree of a vertex v is the number of
edges that have v as the end vertex
the out-degree of a vertex v is the number of
edges that have v as the start vertex
if di is the degree of a vertex i in a graph G
with n vertices and e edges, the number of
edges is n 1

e ( di ) / 2
0

4
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
3 0
0 2
1 2
3 1 2 3 3 3
3 3 4 5 6
3
1 1 1 1
0 in:1, out: 1

1 in: 1, out: 2

2 in: 1, out: 0
5
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
3 2
path: sequence of
vertices v1,v2,. . .vk
such that 3

consecutive vertices
3 3
vi and vi+1 are
adjacent.
a b a b

c c

d e d e
abedc bedc

6
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
simple path: no repeated vertices
cycle: simple path, except that the last
vertex is the same as the first vertex

a b a b

bec adca
c c

d e d e

7
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
connectedgraph:anytwoverticesareconnectedbysomepath

subgraph: subset of vertices and edges forming a


graph
connected component: maximal connected
subgraph. E.g., the graph below has 3 connected
components.

8
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
A weighted graph associates weights
with the edges
e.g., a road map: edges might be
weighted with distance
10

7
8 8
6
6
10

9
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Definitions
We will typically express running
times in terms of |E| and |V|
If |E| |V|2 the graph is dense
If |E| |V| the graph is sparse

If you know you are dealing with


dense or sparse graphs, different
data structures may make sense

10
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Representation

Adjacency Matrix
Adjacency Lists
Incidence Matrix

11
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
Let G=(V,E) be a graph with n vertices.
The adjacency matrix of G is a two-dimensional
nxn array, say adj_mat
If the edge (vi, vj) is in E(G), adj_mat[i][j]=1
If there is no such edge in E(G), adj_mat[i][j]=0

The adjacency matrix for an undirected graph is


symmetric; the adjacency matrix for a directed
graph need not be symmetric

12
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
0 1
a
1 2 2 d 4
3 b c
A 0 1 2 3 3
0 0 1 1 1 A 1 2 3 4
1 1 0 1 1 1 0 1 1 0
2 1 1 0 1 2 0 0 1 0
3 1 1 1 0 3 0 0 0 0
4 0 0 1 0
13
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Matrix
From the adjacency matrix, to determine the connection of
vertices is easy
The degree of a vertex i is the number of 1s in ith row
For a directed graph, the number of 1s in ith row is the out_degree,
while the number of 1s in ith column in_degree.
Time: to list all vertices adjacent to u: O(V).
Time: to determine if (u, v) E: O(1).
Space: O(V2).
Not memory efficient for large graphs.
Parallel edges cannot be represented
Can store weights instead of bits for weighted graph.

14
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Lists
Adjacency list: for each vertex v V,
store a list of vertices adjacent to v
0
0
1 2
3 0 1
1 0 2 1
0 1 2 3
2
1 0 2 3
2 0 1 3 2
3 0 1 2
15
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Lists
typedef struct adjvertex
{
int vertex;
struct node *next;
}adjvertex;

typedef struct graph


{
int no_of_Vertices;
adjvertex *adjlist [100];
}graph;
16
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Adjacency Lists
How much storage is required?
The degree of a vertex v = # incident edges
Directed graphs have in-degree, out-degree
For directed graphs, # of items in adjacency lists
is
out-degree(v) = |E|
takes O(V + E) storage
For undirected graphs, # items in adjacency lists
is
degree(v) = 2 |E|
also O(V + E) storage
So: Adjacency lists take O(V+E) storage
17
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Incidence Matrix
Consider a matrix A = (aij), rows
corresponds to vertices, column
corresponds to edges.
For undirected graph:
aij= 1 if ej is incedent to vi
= 0 otherwise
For directed graph:
aij= 1 if ej is incedent out of vi
= -1 if ej is incedent into vi
= 0 otherwise

18
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Incidence Matrix
0
1 2
3

A (0,1) (0, 2) (0, 3) (1, 2) (1, 3) (2, 3)


0 1 1 1 0 0 0
1 1 0 0 1 1 0
2 0 1 0 1 0 1
3 0 0 1 0 1 1
19
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Traversal
Given: a graph G = (V, E), directed or
undirected
Goal: systematically explore every
vertex and every edge
Ultimately: build a tree on the graph
Pick a vertex as the root
Choose certain edges to produce a tree

20
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Graph Traversal
Depth First Search
Once a possible path is found, continue
the search until the end of the path
Think of a Stack
Breadth First Search
Start several paths at a time, and
advance in each one step at a time
Think of a Queue

21
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
We start at vertex s, and mark s visited.
Next we label s as our current vertex called
u.
Now we travel along an arbitrary edge (u,
v).
If edge (u, v) leads us to an already visited
vertex v we return to u.
If vertex v is unvisited, we move to v, mark
v visited, set v as our current vertex, and
repeat the previous steps.
22
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
void DFS (int start)
{
int v;
adjvertex *adj;
visited [start] = 1;
printf (%d, start);
adj = gadjlist [start];
while (adj ! = NULL)
{
v = adjvertex;
if (! visited [v])
DFS (v);
adj = adj next;
}
}
Total running time: O(2e)=O(e)
23
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

Adjacency Lists
D E
A: F C B G
B: A
C: A
F D: F E
E: G F D
F: A E D
G: E A

24
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

D E
F newly
discovered
F
Undiscovered
visit(A)
Marked
(A, F) (A, C) (A, B) (A, G)
Active
Stack
Finished
25
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

A already
marked B C G

D E

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

26
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

D EE
E newly
discovered

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

27
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

G newly
discovered
B C G

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

28
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

visit(G)
E already B C G
marked (G, E) (G, A)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

29
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

visit(G)
A already B C G
marked (G, E) (G, A)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

30
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
Finished G

visit(G)
B C G (G, E) (G, A)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

31
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

F already B C G
marked

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

32
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

D newly visit(E)
D E
discovered (E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

33
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

F already visit(D)
B C G
marked (D, F) (D, E)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)

Finished Stack

34
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

E already visit(D)
B C G
marked (D, F) (D, E)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)

Finished Stack

35
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A
Finished D

visit(D)
B C G
(D, F) (D, E)

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
Active (A, F) (A, C) (A, B) (A, G)

Finished Stack

36
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

Finished E
B C G

visit(E)
D E
(E, G) (E, F) (E, D)

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

37
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

D already B C G
marked

D E

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

38
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

Finished F
D E

visit(F)
F
(F, A) (F, E) (F, D)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

39
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

C newly
discovered

B C G

D E

F
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

40
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

A already B C G
marked

D E

visit(C)
F
(C, A)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

41
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

Finished C
D E

visit(C)
F
(C, A)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

42
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B newly BB C G
discovered

D E

F
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

43
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

A already B C G
marked

D E

visit(B)
F
(B, A)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

44
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

Finished B
D E

visit(B)
F
(B, A)
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

45
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

G already B C G
finished

D E

F
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished
Stack

46
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

D E

Finished A
F
Undiscovered
Marked visit(A)
(A, F) (A, C) (A, B) (A, G)
Active
Finished Stack

47
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Depth First Search
A

B C G

D E

F
Undiscovered
Marked
Active
Finished
48
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
Breadth-First Search (BFS) traverses a graph, and in
doing so defines a tree with several useful properties.
The starting vertex s has level 0, and, as in DFS,
defines that point as an anchor.
In the first round, all of the edges that are only one
edge away from the anchor are visited.
These vertices are placed into level 1;
In the second round, all the new edges from level 1
that can be reached are visited and placed in level 2.
This continues until every vertex has been assigned a
level.
The label of any vertex v corresponds to the length of
the shortest path from s to v.

49
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
void BFS (int start) {
int v, result; queue q; adjvertex *adj;
visited [start] = 1; enqueue (start, &q);
while ((result = dequeue (&v, &q)) != -1) Nodes
{ dequeued
printf (%d, v); adj = gadjlist [v];
while (adj != NULL) {
if (! visited [adjvertex])
{
visited [adjvertex]Nodes
= 1; enqueued
enqueue (adjvertex, exactly
&q); once
}
adj = adjnext;
} } }
Total running time: O(2e)=O(e)
50
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
2 4 8

s 5 7

3 6 9

51
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered
Top of queue Queue: s
Finished
52
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
2 4 8

0 s 5 7

3 6 9

Undiscovered
Discovered
Top of queue Queue: s 2
Finished
53
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: s 2 3
Finished
54
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: 2 3 5
Finished
55
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: 2 3 5
Finished
56
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

5 already discovered:
0 s 5 7
don't enqueue
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: 2 3 5 4
Finished
57
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: 2 3 5 4
Finished
58
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

Undiscovered
Discovered
Top of queue Queue: 3 5 4
Finished
59
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 3 5 4
Finished
60
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 3 5 4 6
Finished
61
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 5 4 6
Finished
62
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 5 4 6
Finished
63
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 4 6
Finished
64
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 4 6
Finished
65
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 4 6 8
Finished
66
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2

Undiscovered
Discovered
Top of queue Queue: 6 8
Finished
67
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 6 8 7
Finished
68
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 6 8 7 9
Finished
69
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 8 7 9
Finished
70
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 7 9
Finished
71
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 7 9
Finished
72
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 7 9
Finished
73
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 7 9
Finished
74
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 9
Finished
75
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 9
Finished
76
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue: 9
Finished
77
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

Undiscovered
Discovered
Top of queue Queue:
Finished
78
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Breadth First Search
1 2 3
2 4 8

0 s 5 7
1 3

3 6 9

1 2 3

BFS Tree

79haskar Sardar, Information Technology Department, Jadavpur University, India 79


B
Single Source Shortest Path
Problem
The problem of finding shortest paths from
a source vertex v to all other vertices in
the graph.

80
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm
solution to the single-source shortest path problem in graph
theory.

Works on both directed and undirected graphs. However, all


edges must have nonnegative weights.

Approach: Greedy : makes local optimum choice in each step


hoping to reach global optimum.

Input: Weighted graph G={E,V} and source vertex vV, such


that all edge weights are nonnegative

Output: Lengths of shortest paths (or the shortest paths


themselves) from a given source vertex vV to all other vertices

81
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm
dist[s] 0 (distance to source vertex is zero)
for all v V{s}
do dist[v] (set all other distances to infinity)
S (S, the set of visited vertices is initially empty)
QV (Q, the queue initially contains all vertices)
while Q (while the queue is not empty)
do u mindistance(Q,dist) (select the element of Q with the min. distance)
SS{u} (add u to list of visited vertices)
for all v neighbors[u]
do if dist[v] > dist[u] + w(u, v) (if new shortest path found)
then d[v] d[u] + w(u, v) (set new value of shortest path)
return dist

Total running time: O(n2)


82
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

83
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

84
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

85
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

86
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

87
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

88
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

89
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

90
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

91
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Dijkstras Algorithm

92
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Spanning Trees

A spanning tree of a graph is a subgraph that contains all


the vertices and is a tree.
A graph may have many spanning trees.

Graph A Some Spanning Trees from Graph A

or or or

93
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Minimum Spanning Trees
The Minimum Spanning Tree for a given graph is the Spanning Tree of
minimum cost for that graph.

Graph Minimum Spanning Tree


7
2 2
5 3 3
4
1 1

7 7 7 7

7 7

94
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Algorithms for Obtaining the Minimum Spanning Tree

Kruskal's Algorithm

Prim's Algorithm

95
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Kruskal's Algorithm

o This algorithm creates a forest of trees.


o Initially the forest consists of n single node trees (and no edges).
o At each step, we add one edge (the cheapest one) so that it joins two
trees together.
o If it were to form a cycle, it would simply link two nodes that were
already part of a single connected tree, so that this edge would not be
needed.

96
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Kruskal's Algorithm
The steps are:

1. The forest is constructed - with each node in a separate tree.


2. Sort the edges.
3. Until we've added n-1 edges,
3.1. Extract the next cheapest edge.
3.2. If it forms a cycle, reject it.
3.3. Else add it to the forest. Adding it to the forest will join
two trees together.

Every step will join two trees in the forest together, so that at the
end, there will only be one tree in T.
97
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph

B 4 C
4
2 1

A 4 E
1 F

D 2 3
10
G 5

5 6 3

4
I
H

2 3
J

98
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
A 4 B A 1 D

B 4 C B 4 D

B 4 C B 10 J C 2 E
4
2 1
C 1 F D 5 H
A 4 E
1 F

2 D 6 J E 2 G
D 3
10
G 5
F 3 G F 5 I
5 6 3

4
I G 3 I G 4 J
H

2 3
J H 2 J I 3 J

99
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Sort Edges A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

100
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

101
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

102
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

103
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

104
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

105
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Cycle
A 1 D C 1 F
Dont Add Edge

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

106
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

107
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

108
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

109
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Cycle
A 1 D C 1 F
Dont Add Edge

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

110
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Add Edge A 1 D C 1 F

C 2 E E 2 G

B 4 C H 2 J F 3 G
4
2 1
G 3 I I 3 J
A 4 E
1 F

2 A 4 B B 4 D
D 3
10
G 5
B 4 C G 4 J
5 6 3

4
I F 5 I D 5 H
H

2 3
J D 6 J B 10 J

111
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Minimum Spanning Tree Example Graph

B 4 C 4
B C
4 4
2 1 2 1
A E A 4
1 F E F
1

D 2 2
D 3
10
G G 5

3 5 6 3

4
I I
H H
2 3 3
J 2 J

112
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Analysis of Kruskal's Algorithm

The algorithm starts with sorting edges (O (E log E)).

We consider all E edges.

For each edge we check for possibility of cycle (O(logn))

Total running time is O(ElogE) + O (Elogn)

113
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Prim's Algorithm

This algorithm starts with one node.


It then, one by one, adds a node that is unconnected to the new
graph to the new graph.
Each time selects the node whose connecting edge has the
smallest weight out of the available nodes connecting edges.

114
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Prim's Algorithm
The steps are:

1. The new graph is constructed - with one node from the old
graph.
2. While new graph has fewer than n nodes,
2.1. Find the node from the old graph with the smallest
connecting edge to the new graph,
2.2. Add it to the new graph 2
Complexity: O(n )
In every step one node is added, so that at the end we will have
one graph with all the nodes and it will be a minimum spanning
tree of the original graph.

115
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph

B 4 C
4
2 1

A 4 E
1 F

D 2 3
10
G 5

5 6 3

4
I
H

2 3
J

116
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

117
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

118
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

119
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

120
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

121
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

122
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

123
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

124
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

125
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph New Graph

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A 4
1 F E F
1

D 2 3 2
D 3
10 10
G 5 5
G
5 6 3 5 6 3
4 4
I I
H H
2 3 3
J 2 J

126
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Example Graph Minimum Spanning Tree

B 4 C 4
B C
4 4
2 1 2 1
A 4 E A
1 F E F
1

D 2 3 2
D
10
G 5
G
5 6 3 3
4
I I
H H
2 3 3
J 2 J

127
Bhaskar Sardar, Information Technology Department, Jadavpur University, India
Any Doubt ?
Please feel
free to write
to me:

bhaskargit@yahoo.
co.in

128
Bhaskar Sardar, Information Technology Department, Jadavpur University, India

Вам также может понравиться