1.13k likes | 1.35k Views
CS 235102 Data Structures ( 資料結構 ). Chapter 6: Graph Algorithms Spring 2012. Seven Bridge. Euler: the first recorded use of graph land : A 、 B 、 C 、 D bridge: a 、 b 、 c 、 d 、 e 、 f 、 g. Seven Bridge.
E N D
CS 235102 Data Structures (資料結構) Chapter 6: Graph Algorithms Spring 2012
Seven Bridge • Euler: the first recorded use of graph • land : A、B、C、D • bridge: a、b、c、d、e、f、g
Seven Bridge • Is there a path going through each bridge exactly once?(starting and ending at the same node) => iff the degree of each vertex is even. • graph modeling • circuit, job ordering, shortest path, ……
Undirected graph • G=(V,E) • V : a set of vertices. • E : a set of pairs of vertices called edges. • Example: • V(G) = { 1, 2, 3, 4 } • E(G) = { (1,2), (1,3), (1,4),(2,3), (2,4), (3,4) } 1 G: 2 4 3
Terminology • Complete graph: • A graph with n(n-1)/2 edges • Example: • Edge: • (u,v) is an edge, if u and v are adjacent. • Example: • In G, 1 and 2 are adjacent, then (1,2) is an edge. 1 G : 2 4 3
Terminology • Subgraph: • G’ is a subgraph of G such that V(G’) V(G) and E(G’) E(G). • Example: G : G’ : 1 1 2 4 2 4 3 3 G’ is a subgraph of G
Terminology • Path: • A path v1~vn represent that (v1,v2), (v2,v3),…, (vn-1,vn) are edges. • Example: • Simple path: • A simple path is a path which all vertices except possibly the first and the last are distinct. • Example: • 1-2-3-4-2-1 • 1-2-4-3-1 1 2 4 1-2-4 is a path Wrong!!! Correct!!!
Terminology • Cycle: • A cycle is a simple path which the first and the last vertices are same • Example: 2 1 1-2-4-3-1 is a cycle 3 4
Terminology • Connected component: • There is always a path between every pair of vertices. • Example: • Tree: • A connected component without cycle. 1 1 2 4 2 4 3 3 Wrong!!! Correct!!!
Terminology • Simple graph: • For any pair of vertices (u,v), there is at most one edge. • Multigraph: • Not a simple graph • Example: u v
Terminology • Degree of a vertex: • The number of edges incident to the vertex. • Example: • Let d(i) be the degree of vertex i 1 G : deg(2) = 2 2 3
Graph representation • Two kinds of graph representation • Adjacency matrix • Adjacency list
Graph representation • Adjacency matrixof G • A two dimensional array with the property that A[i,j] = 1 iff the edge (i,j) is in E(G). • Example: • Disadvantage: • Waste of memory if it is a sparse graph. 1 2 3 4 symmetric
Graph representation • Adjacency listof G • Example: • Storage = O(n+2e) ( let |E| = e, |V| = n ) 2 1 null 3 1 1 2 3 4 null 2 3 2 1 null 3 4 2 4 null
Operations • Three operations on a graph G. • Graph traversal • Spanning Tree • Minimun cost spanning tree
Graph traversal • There are two kinds of graph traversal operations. • Depth-First-Search (DFS) • Breath-First-Search (BFS)
Graph traversal-DFS • Depth-First-Search (DFS) • Starting at any node v • DFS(v): visit the node v • DFS(w): for each vertex w adjacent to v, if w is not visited then visit w. • …… • Example 1: 1 1 2 2 3 3 1 → 2 → 4 → 5 → 6 → 3 4 4 5 5 6 6
Graph traversal-DFS • Example 2: 1 1 2 2 3 3 1 → 2 → 4 → 8 → 5 → 6 → 3 → 7 4 4 5 5 6 6 7 7 8 8
Graph traversal – DFS (Recursive) DFS(v) { visit[v] = true; for ( all vertices w adjacent to v ) { if ( not visit[w] ) { DFS(w); } } }
Graph traversal – DFS (Iterative) DFS(v) // need a stack to implement DFS { push(S,v); // push v into stack S while ( not empty (S) ) { v = pop(S); if ( not visit[v] ) { visit[v] = true; for ( all vertices w adjacent to v ) { if ( not visit[w] ) { push(S,w); } } } } }
Graph traversal-DFS • Time complexity for DFS • O(n2) ( use adjacency matrix ) • O(e) ( use adjacency list )
Graph traversal-BFS • Breath-First-Search (BFS) • Starting at any node v • visit the starting vertex v • visit all unvisited vertices w adjacent to v • …… • Example 1: 1 1 1 → 2 → 6 → 3 → 4 → 5 2 2 3 3 4 4 5 5 6 6
Graph traversal- BFS • Example 2: 1 1 2 2 3 3 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 4 4 5 5 6 6 7 7 8 8
Graph traversal - BFS BFS(v) //need a queue to implement BFS { visit[v] = true; addq(q,v); //add v into queue q while ( not empty (q) ) { v = delete(q); for ( all vertices w adjacent to v ) { if ( not visit[w] ) { visit[w] = true; addq(q,w); } } } }
Graph traversal - BFS • Time complexity for BFS • O(n2) ( use adjacency matrix ) • O(e) ( use adjacency list )
Spanning tree • We will introduce two kinds of spanning trees. • DFS spanning tree • BFS spanning tree
Spanning tree • Spanning tree of a graph G • Any tree consists of solely edges in G and include all vertices in G. • Example: G : spanning trees of G
Spanning tree • Properties of a tree • |E| = n-1 • Introducing an edge into the tree will create a cycle. • Connected component with n-1 edges.
Spanning tree • DFS spanning tree • Tree edges: the edge traversed.Non-tree edges: the edge not traversed. • Example: 1 1 1 2 2 3 3 2 3 4 4 5 5 6 6 7 7 4 5 6 7 8 8 8
Spanning tree • DFS spanning tree still satisfies tree’s property. • A connected component with n-1 edges. • If a non-tree edge is introduced into any spanning tree, a cycle is formed. 1 2 3 4 5 6 7 8
Spanning tree • BFS spanning tree • Example: • The shortest distance from node 1 to other vertices (edge cost = 1 ) 1 1 1 2 2 3 3 2 3 4 4 5 5 6 6 7 7 4 5 6 7 8 8 8
Minimun cost spanning tree • Minimun cost spanning tree of a graph G • Edge represents communication link.Weights on the edges represent the cost of the link. • Total weights of the selected edge are minimun. • Example 1: A A 5 5 B D B D 3 3 10 7 9 9 E C E C 8 8 Minimun cost spanning tree
Minimun cost spanning tree • Example 2: A A 6 5 1 1 B B D D 5 C C 5 5 2 2 6 3 3 4 4 F E F E 6 Minimun cost spanning tree
Minimun cost spanning tree • Three algorithmns • Kruskal’s algorithm • Prim’s algorithm • Sollin’s algorithm
Kruskal’s algorithm • Kruskal’s algorithm • Idea: • Step(1): Find the minimum cost edge each time. • Step(2): If it creates a cycle, discard the edge. • Step(3): Repeat step(1)(2), until we find n-1 edges.
Kruskal’s algorithm • Example: • w(1,6) = 10 • w(3,4) = 12 • w(2,7) = 14 • w(2,3) = 16 • w(4,7) = 18 • w(4,5) = 22 • w(5,7) = 24 • w(5,6) = 25 • ← cycle 1 1 28 • ← cycle 2 10 2 10 14 14 16 16 6 7 6 7 3 3 18 24 25 12 25 12 4 5 4 5 22 22
Kruskal’s algorithm • Kruskal’s algorithm • T = ψ • While ( |T| < n-1 ) • { • choose an edge (v,w) from E of least cost • delete (v,w) from E • if ( (v,w) does not create a cycle ) • then add (v,w) to T • else discard (v,w) • } • Step(4)(5): • Use min heap to store edge cost • One step: O(log e) • Total: O(e log e)
Kruskal’s algorithm • Step(6)(7): • Use the set representation • use function Union& Find • Group all vertices in the same connected component into a set. • For an edge (v,w) to be added • Find(v)、 Find(w) • If the two vertices of an edge are in the same set => It forms a cycle
Kruskal’s algorithm • Example: 1 1 2 3 4 5 7 6 6 1 28 2 10 14 • Add edge(1,6) of least cost • Find(1)、Find(6) • Union(1,6) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 1 2 3 4 5 7 6 1 28 2 10 14 • Add edge(1,6) of least cost • Find(1)、Find(6) • Union(1,6) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 3 3 4 4 5 7 6 1 28 2 10 14 • Add edge(3,4) of least cost • Find(3)、Find(4) • Union(3,4) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 3 3 5 7 6 4 1 28 2 10 14 • Add edge(3,4) of least cost • Find(3)、Find(4) • Union(3,4) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 2 3 5 7 7 6 4 1 28 2 10 14 • Add edge(2,7) of least cost • Find(2)、Find(7) • Union(2,7) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 2 3 5 6 7 4 1 28 2 10 14 • Add edge(2,7) of least cost • Find(2)、Find(7) • Union(2,7) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 2 3 3 5 6 7 4 1 28 2 10 14 • Add edge(2,3) of least cost • Find(2)、Find(3) • Union(2,3) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 2 5 6 7 3 1 28 2 10 14 4 • Add edge(2,3) of least cost • Find(2)、Find(3) • Union(2,3) 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 5 6 7 7 3 1 28 2 10 • Add edge(4,7) of least cost • Find(4)、Find(7) • 4、7 in the same set ( => create a cycle) • Discard edge(4,7) 14 4 4 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm Example: 1 2 5 6 7 7 3 1 28 2 10 • Add edge(4,7) of least cost • Find(4)、Find(7) • 4、7 in the same set ( => create a cycle) • Discard edge(4,7) 14 4 4 16 6 7 3 18 24 25 12 4 5 22
Kruskal’s algorithm • Step(6)(7): • Use the set representation • Time one step = O( α(e) )where α(e) < log e • Total time for this part: O(e α(e) )
Kruskal’s algorithm • Time Complexity • Step(4)(5): O(log e) • Step(6)(7): O(α(e)) • Each round: O(log e) • Total execute erounds • Total time complexity: O(e loge)