Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph.

Similar presentations


Presentation on theme: "Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph."— Presentation transcript:

1 Chapter 6 張啟中

2 Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph

3 Applications of Graphs Analysis of electrical circuits Finding shortest routes Project planning Identification of chemical compounds Statistical mechanics Generics Cybernetics Linguistics Social Sciences

4 Definitions A graph G=(V,E), consists of two sets.  V is a finite, nonempty set of vertices. V(G)  E is a set of pairs of vertices, these pairs are call edges. V(E) Graphs  Undirected graph (graph) edge (u,v) = (v,u)  Directed graph (digraph) edge ≠

5 Example: Graphs 0 3 12 0 1 34 2 56 0 1 2 (a) G 1 (b) G 2 (c) G 3 V(G 1 ) = {0, 1, 2, 3} E(G 1 ) = {(0, 1), (0, 2), (0, 3), (1, 2), (1, 3), (2, 3)} V(G 2 ) = {0, 1, 2, 3, 4, 5, 6} E(G 2 ) = {(0, 1), (0, 2), (1, 3), (1, 4), (2, 5), (2, 6)} V(G 3 ) = {0, 1, 2} E(G 3 ) = {,, } Directed Graph Undirected Graph

6 Restrictions on Graphs No self edges (self loops)  A graph may not have an edge from a vertex, v, back to itself. No multigraph  A graph may not have multiple occurrences of same edges. 0 2 1 1 2 3 0 (a) Graph with a self edge(b) Multigraph

7 Complete Graph The number of distinct unordered pairs (u, v) with u≠v in a graph with n vertices is n(n-1)/2. Complete graph  Undirected graph an n-vertex graph with exactly n(n-1)/2 edges.  Directed graph an n-vertex graph with exactly n(n-1) edges K4K4 K3K3

8 Adjacent and Incident Undirected graph  If (u, v) is an edge in E(G), vertices u and v are adjacent and the edge (u, v) is the incident on vertices u and v. Directed graph  indicates u is adjacent to v and v is adjacent from u.  is incident to u and v. 0 1 34 2 56 0 1 2

9 Subgraph A subgraph of G is a graph G’ such that V(G’) V(G) and E(G’) E(G). 0 3 12 0 3 12 0 12 0 0 1 2 0 1 2 0 1 2 0 1 2 (5) (6)(7)(8) 0 3 12 (1) (2) (3) (4)

10 Path and Length Path  A path from vertex u to vertex v in graph G is a sequence of vertices u, i 1, i 2, …, i k, v, such that (u, i 1 ), (i 1, i 2 ), …, (i k, v) are edges in E(G).  G’ is directed graph,,, …, are edges in E(G’). Length  The length of a path is the number of edges on it. Simple Path  A simple path is a path in which all vertices except possibly the first and last are distinct. A path (0, 1), (1, 3), (3, 2) can be written as 0, 1, 3, 2.

11 Cycle A cycle is a simple path in which the first and last vertices are the same. Similar definitions of path and cycle can be applied to directed graphs.

12 Example: Path, Simple Path, Cycle 0 1 2 4 3 5 Path 1 (0,1) (1,3) (3,4) (4,2) (2,1) (1,4) (4,5) Path 2 (0,1) (1,3) (3,4) (4,5) Path 3 (1,3) (3,4) (4,2) (2,1) Simple Path Path 2 、 Path 3 Circle Path 3

13 Connected and Connected Component Two vertices u and v are connected in an undirected graph iff there is a path from u to v (and v to u). An undirected graph is connected iff for every pair of distinct vertices u and v in V(G) there is a path from u to v in G. A connected component of an undirected is a maximal connected subgraph. A tree is a connected acyclic graph

14 Strongly Connected Graph A directed graph G is strongly connected iff for every pair of distinct vertices u and v in V(G), there is directed path from u to v and also from v to u. A strongly connected component is a maximal subgraph that is strongly connected.

15 Example: Connected and Strongly Connected 0 3 12 4 7 56 G4G4 H1H1 H2H2 0 1 2 0 1 2

16 Degree of A Vertex Undirected graph  The degree of a vertex is the number of edges incident to that vertex. Directed graph  In-degree The number of edges for which vertex is the head  Out-degree The number of edges for which the vertex is the tail. For a graph G with n vertices and e edges, if d i is the degree of a vertex i in G, then the number of edges of G is

17 Articulation Point A vertex v of G is an articulation point iff the deletion of v, together with the deletion of all edges incident to v, leaves behind a graph that has at least two connected components.

18 Biconnected graph and Biconected Component Biconnected graph  A biconnected graph is a connected graph that has no articulation points. Biconected component  A biconnected component of a connected graph G is a maximal biconnected subgraph H of G. Two biconnected components of the same graph can have at most one vertex in common. No edge can be in two or more biconnected components. The biconnected components of G partition the edges of G.

19 Example: Biconnected 0 1 4 23 8 7 6 5 9 (a)A connected graph 1,3,5,7 is articulation point 1 4 23 7 6 5 0 1 35 8 7 7 9 (b) Its biconnected components

20 Weighted Edges Very often the edges of a graph have weights associated with them.  distance from one vertex to another  cost of going from one vertex to an adjacent vertex.  To represent weight, we need additional field, weight, in each entry.  A graph with weighted edges is called a network.

21 Graph Representations ADT (Please see book p.336) Adjacency Matrix Adjacency Lists Adjacency Multilists

22 Graph ADT class Graph { // Objects: A nonempty set of vertices and a set of undirected edges where each edge is a pair of vertices public: Graph(); // Create an empty graph void InsertVertex(Vertex v); void InsertEdge(Vertex u, Vertex v); void DeleteVertex(Vertex v); void DeleteEdge(Vertex u, Vertex v); //if graph has no vertices return TRUE Boolean IsEmpty(); List Adjacent(Vertex v); //return a list of all vertices that are adjacent to v };

23 Adjacency Matrix Adjacency Matrix is a two dimensional n×n array. (a) G 1 (b) G 3 0 3 12 0 1 2

24 Adjacency Matrix (c) G 4 0 3 12 4 7 56 G4G4 H1H1 H2H2

25 Adjacency Lists 3 2 1 0 1 3 3 1 20 00 00 20 [0] [1] [2] [3] 0 10 200 [0] [1] [2] HeadNodes (a) G 1 (b) G 3 0 3 12 0 1 2

26 Adjacency Lists 2 3 0 1 10 00 30 10 [0] [1] [2] [3] HeadNodes (c) G 4 50 6 5 60 40 70 [4] [5] [6] [7] 0 3 12 4 7 56 G4G4 H1H1 H2H2

27 Adjacency Lists : Sequential Representation 9111315171820222321300312564576 0 1 23 4 5 6 7 8 9 10 11 12 13 141516 17 1819 202122 0 3 12 4 7 56 G4G4 H1H1 H2H2

28 Inverse Adjacency Lists 10 00 10 [0] [1] [2] 0 1 2 0 10 200 [0] [1] [2]

29 Adjacency Lists: Orthogonal List 0 1 20 100 0 0100 12 1200 head nodes (shown twice) tail column link for head head row link for tail 0 1 2

30 Multilists [0] [1] [2] [3] 01N1N3 02N2N3 030N4 12 N5 130 2300 N0 N1 N2 N3 N4 N5 HeadNodes edge (0, 1) edge (0, 2) edge (0, 3) edge (1, 2) edge (1, 3) edge (2, 3) The lists are Vertex 0: N0 -> N1 -> N2 Vertex 1: N0 -> N3 -> N4 Vertex 2: N1 -> N3 -> N5 Vertex 3: N2 -> N4 -> N5 m vertax2 vertex1 list1list2 0 3 12

31 Graph Operations Graph Traversals  A general operation on a graph G is to visit all vertices in G that are reachable from a vertex v. Depth-First Search Breadth-First Search Graph Components  Connected Components  Spanning Trees  Biconnected Components

32 Depth-First Search 0 7 1 34 2 56 1 0 0 1 1 2 2 3 02 3 5 07 07 07 07 4 04 06 506 [0] [1] [2] [3] [4] [5] [6] [7] HeadNodes 0,1,3,7,4,5,2,6

33 Breadth-First Search 0 7 1 34 2 56 1 0 0 1 1 2 2 3 02 3 5 07 07 07 07 4 04 06 506 [0] [1] [2] [3] [4] [5] [6] [7] HeadNodes 0,1,2,3,4,5,6,7

34 DFS and BFS Performance Analysis Representation Search adjacency matrixadjacency lists DFSO(n 2 )O(e) BFSO(n 2 )O(e)

35 Connected Components The connected components of a graph may be obtained by making repeated calls to either DFS(v) or BFS(v) G is represented by adjacency lists, it take O(n+e) to generate all the connected components. If adjacency matrices are used instead, the time required is O(n 2 )

36 Spanning Trees: Complete Graph

37 DFS and BFS Spanning Tree 34 12 56 0 7 341256 0 7 (a) DFS (0) spanning tree(b) BFS (0) spanning tree

38 Proporety of Spanning Trees Any tree consisting solely of edges in G and including all vertices in G is called a spanning tree A spanning tree is a minimal subgraph, G’, of G such that V(G’) = V(G), and G’ is connected. (Minimal subgraph is defined as one with the fewest number of edges). Spanning tree can be obtained by using either a depth-first or a breath-first search. Any connected graph with n vertices must have at least n-1 edges, and all connected graphs with n – 1 edges are trees. Therefore, a spanning tree has n – 1 edges. When a nontree edge (v, w) is introduced into any spanning tree T, a cycle is formed. Question: Do G have spanning trees if G is not connected ?

39 Find Biconnected Components The root of the depth-first spanning tree is an articulation point iff it has at least two children. Any other vertex u is an articulation point iff it has at least one child, w, such that it is not possible to reach an ancestor of u using apath composed solely of w, descendants of w, and a single back edge. Define low(w) as the lowest depth-first number that can be reached fro w using a path of descendants followed by, at most, one back edge.

40 Find Biconnected Components u is an articulation point iff u is either the root of the spanning tree and has two or more children or u is not the root and u has a child w such that low(w) ≥ dfn(u).

41 Find Biconnected Components 0 1 4 23 8 7 6 5 9 1 2 3 4 5 6 7 8 9 10 3 4 2 1 0 6 7 8 5 9 1 2 3 4 5 6 7 8 9 vertex 0123456789 dfn 54312678109 low 51111666109

42 Minimum-Cost Spanning Trees The cost of a spanning tree of a weighted, undirected graph is the sum of the costs (weights) of the edges in the spanning tree. A minimum-cost spanning tree is a spanning tree of lease cost. Greedy Algorithm  Kruskal’s Algorithm  Prim’s Algorithm  Sollin’s Algorithm

43 Kruskal ’ s Algorithm 0 5 1 6 4 3 2 10 28 14 16 12 18 22 25 (a)(b) 24 0 5 1 6 4 3 2 10 14 16 12 22 25

44 Kruskal ’ s Algorithm 1. T = {}; 2. while ((T contains less than n-1) && (E not empty)) { 3 Choose an edge (v,w) from E of lowest cost; 4. Delete (v,w) from E 5. If ((v,w) does not creste a cycle in T) add (v,w) to T else discard (v,w); 6. } 7. If (T contains fewer than n-1 edges) 8. cout << “no spanning tree” << endl;

45 Prim ’ s Algorithm 0 5 1 6 4 3 2 10 28 14 16 12 18 22 25 24 (a) (b) 0 5 1 6 4 3 2 10 14 16 12 22 25

46 Prim ’ s Algorithm //Assume that G has at least one vertex 1. TV = {}; //start with vertex 0 and no edges 2. for (T={}; T contains less than n-1; add (u,v) to T) { 3 Let (v,w) be a least-cost edge such that u in TV and v not in TV; 4. If (there is no such edge) break; add v to TV; 5. } 6. If (T contains fewer than n-1 edges) 7. cout << “no spanning tree” << endl;

47 Sollin ’ s Algorithm 0 5 1 6 4 3 2 10 28 14 16 12 18 22 25 24 (a) (b) 0 5 1 6 4 3 2 10 14 16 12 22 25

48 Performance Analysis Algorithm Item KruskalPrimSollin Time Complexity O( eloge )O(n 2 )

49 Shortest Paths Single source/all destinations: Nonnegative edges costs. Single Source/all destinations: General Weights. All-Pairs Shortest Paths

50 Single source/all destinations: Nonnegative edges costs 0 34 1 2 5 20 50 15 20 10 35 30 3 15 Path Length 1) 0, 3 2) 0, 3, 4 3) 0, 3, 4, 1 4) 0, 2 10 25 45 (a) Graph(b) Shortest paths from 0

51 Single source/all destinations: Nonnegative edges costs 0 1 2 3 4 5 6 7 300 800 1700 1000 1400 1200 1500 1000 250 900 Los Angeles San Francisco Denver New Orleans Mia mi New York Boston Chicago

52 Single source/all destinations: Nonnegative edges costs iterationS Vertex selected Distance LA SFDENCHIBOSTNYMIANO [0] [1][2][3][4][5][6][7] Initial----- +∞+∞ +∞+∞ +∞+∞ 15000250+∞+∞ +∞+∞ 1{4}5 +∞+∞ +∞+∞ +∞+∞ 1250025011501650 2{4,5}6 +∞+∞ +∞+∞ +∞+∞ 1250025011501650 3{4,5,6}3 +∞+∞ +∞+∞ 24501250025011501650 4{4,5,6,3}7 3350 +∞+∞ 24501250025011501650 5{4,5,6,3,7}2 3350 325024501250025011501650 6{4,5,6,3,7,2}1 3350 325024501250025011501650 {4,5,6,3,7,2,1}

53 Single Source/All Destinations: General Weights When negative edge lengths are permitted, we require that the graph have no cycles of negative length. 012 5 7 -5 012 -2 11 (a) Directed graph with a negative-length edge (b) Directed graph with a cycle of negative length

54 Single Source/All Destinations: General Weights 0 1 2 3 4 5 6 (a) A directed graph k dist k [7] 0123456 10655 ∞∞∞ 2033554 ∞ 30135247 40135045 50135043 60135043 (b) dist k 6 5 5 -2 1 3 3

55 All-Pairs Shortest Paths A -1 012 00411 1602 23∞0 A0A0 012 004 1602 2370 A1A1 012 0046 1602 2370 2 01 6 2 4 3 A2A2 012 0046 1502 2370 (b) A -1 (c) A 0 (d) A 1 (e) A 2

56 Transitive Closure Definition  The transitive closure matrix, denoted A +, of a graph G, is a matrix such that A + [i][j] = 1 if there is a path of length > 0 fromi to j; otherwise, A * [i][j] = 0. Definition  The reflexive transitive closure matrix, denoted A *, of a graph G, is a matrix such that A * [i][j] = 1 if there is a path of length 0 from i to j; otherwise, A * [i][j] = 0.

57 Transitive Closure 0123 4 (a) Digraph G (b) Adjacency matrix A (c) A + (d) A *

58 Activity Networks Activity-on-Vertex (AOV) Networks  A directed graph G in which the vertices represent tasks or activities and the edges represent precedence relations between tasks is an activity- on-vertex network or AOV network. Activity-on-Edge (AOE) Networks  A directed graph G in which the edges represent tasks or activities and the vertices represent events. Events signal the completion of certain activities.

59 Activity-on-Vertex (AOV) Networks Vertex i in an AOV network G is a predecessor of vertex j iff there is a directed path from vertex i to vertex j. i is an immediate predecessor of j iff is an edge in G. If i is a predecessor of j, then j is an successor of i. If i is an immediate predecessor of j, then j is an immediate successor of i.

60 Partial Order and Topological Order Partial Order  A relation · is transitive iff it is the case that for all triples, i, j, k, i.j and j · k => i · k. A relation · is irreflexive on a set S if for no element x in S it is the case that x · x. A precedence relation that is both transitive and irreflexive is a partial order. Topological Order  A topological order is a linear ordering of the vertices of a graph such that, for any two vertices I and j, if I is a predecessor of j in the network, then i precedes j in the linear ordering.

61 Example: AOV Network Course numberCourse namePrerequisites C1Programming INone C2Discrete MathematicsNone C3Data StructuresC1, C2 C4Calculus INone C5Calculus IIC4 C6Linear AlgebraC5 C7Analysis of AlgorithmsC3, C6 C8Assembly LanguageC3 C9Operating SystemsC7, C8 C10Programming LanguagesC7 C11Compiler DesignC10 C12Artificial IntelligenceC7 C13Computational TheoryC7 C14Parallel AlgorithmsC13 C15Numerical AnalysisC5 Courses needed for a computer science degree

62 Example: AOV Network C1 C2 C3 C5C4C6C15 C7 C8 C13 C12 C10 C9 C14 C11 C1, C2, C4, C5, C3, C6, C8, C7, C10, C13, C12, C14, C15, C11, C9 C4, C5, C2, C1, C6, C3, C8, C15, C7, C9, C10, C11, C12, C13, C14

63 Example: Topological Order 0 1 2 3 4 5 1 2 3 4 5 1 24 5 1 4 5 1 4 4 (a) Initial (b) Vertex 0 deleted (c) Vertex 3 deleted (d) Vertex 2 deleted (e) Vertex 5 deleted (f) Vertex 1 deleted 0, 3, 2, 5,1, 4

64 Topological Sorting Algorithm with Internal Representation 1 40 4 5 2 50 40 [0] [1] [2] [3] [4] 0 1 1 1 3 0 2 0 [5] 30 count first datalink

65 Example: AOE 1 04 2 6 8 7 3 5 start finish a 1 = 6 a 4 = 6 a 2 = 4 a 5 = 1 a 7 = 9 a 10 = 2 a 3 = 5 a 6 = 2 a 9 = 4 a 11 = 4 eventinterpretation 0Start of project 1Completion of activity a 1 4Completion of activities a 4 and a 5 7Completion of activities a 8 and a 9 8Completion of project

66 AOE Representation with Adjacency Lists [0] [1] [2] [3] [4] 0 1 1 1 3 2 [5] count first vertex 2 [6] 2 [7] 2 0 [8] 16 41 0 41 0 52 0 69 74 0 82 0 84 0 dur link 24 35 0 77 0

67 Computation Early and Late Activity Time ee[0][1][2][3][4][5][6][7][8]Stack Initial000000000[0] output 0064500000[3,2,1] output 3064507000[5,2,1] output 50645070110[2,1] output 20645070110[1] output 10645570110[4] output 40645770140[7,6] output 7064577161418[6] output 6064577161418[8] output 8


Download ppt "Chapter 6 張啟中. Kongsberg Bridge Problem (1736) A Kneiphof a b c d g C D B f e a b c d g e f A B C D Euler’s graph."

Similar presentations


Ads by Google