Uploaded by sujeetkumarnwd61

8.Graph (1)

advertisement
Graph terminology - overview
• A graph G consists of vertices and edges. It is denoted by
G = (V, E) where V = set of vertices: [1,2,3,4], and E =
set of edges:[1,2], [1,3] , [2,4], [3,4]
A tree is a connected graph with no cycles
• Dense graph: |E| ≈ |V|2; Sparse graph: |E| ≈ |V|
• Undirected graph: Edge (u,v) = edge (v,u)
No self-loops
• Directed graph: Edge (u,v) goes from vertex u to vertex v,
u→v
• A weighted graph associates weights with either the edges
or the vertices
Minimum Spanning Tree
• A spanning tree T of a connected graph G is a subgraph of G and T
includes all the vertices of G. More than one T is possible of G.
• In a weighted graph, the weight of a subgraph is the sum of the
weights of the edges in the subgraph.
• A minimum spanning tree (MST) for a weighted graph is a spanning
tree with minimum weight. The sum of the weights of the edges is
unique but not MST.
•Undirected graph
•Minimum spanning tree
MST Algorithms
• Minimum Spanning Tree obtained by greedy algorithm:
Kruskal and Prim’s Algorithms
• Kruskal’s Algorithm: Work with edges, rather than nodes
• Two steps:
– Sort edges by increasing edge weight
– Select the first |V| – 1 edges that do not generate a cycle
Complexity: O(nlog n)—using heap
Example
F
10
A
Consider an undirected, weight graph
3
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
F
10
A
Sort the edges by increasing edge weight
3
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv
edge
dv
(D,E)
1
(B,E)
4
(D,G)
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv
(B,E)
4
2
(B,F)
4
(E,G)
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)

Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

(B,F)
4
3
(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
2
(E,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3
(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Accepting edge (E,G) would create a cycle
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3
(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3
(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4
(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4
2

(B,F)
4
(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)

Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4
(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5
(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
10
A
3
F
C
4
3
4
8
5
6
B
4
D
4
H
1
2
3
G
3
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Select first |V|–1 edges which do not
generate a cycle
3
F
C
A
3
4
5
B
D
H
1
2
3
G
E
edge
dv

(B,E)
4

2

(B,F)
4

(E,G)
3

(B,H)
4

(C,D)
3

(A,H)
5

(G,H)
3

(D,F)
6
(C,F)
3

(A,B)
8
(B,C)
4

(A,F)
10
edge
dv
(D,E)
1
(D,G)
Done
Total Cost =  dv = 21
}
not
considered
Download