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.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CHAPTER 6 GRAPHS All the programs in this file are selected from
1 Slides based on those of Kenneth H. Rosen Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Graphs.
Konigsberg Bridge Problem
Graph Theory.
Chapter 6. Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river.
Minimum cost spanning tree and activity networks Data structure 2002/12/2.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
Chapter 8, Part I Graph Algorithms.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Data Structures Using C++
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS Data Structures Chapter 6 Graphs.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
GRAPH Learning Outcomes Students should be able to:
Data Structures Using C++ 2E
IS 2610: Data Structures Graph April 5, 2004.
Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Directed graphs Definition. A directed graph (or digraph) is a pair (V, E), where V is a finite non-empty set of vertices, and E is a set of ordered pairs.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Theoretic Concepts. What is a graph? A set of vertices (or nodes) linked by edges Mathematically, we often write G = (V,E)  V: set of vertices,
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
1 ELEC692 Fall 2004 Lecture 1b ELEC692 Lecture 1a Introduction to graph theory and algorithm.
Konigsberg Bridge Problem A river Pregel flows around the island Keniphof and then divides into two. Four land areas A, B, C, D have this river on their.
Foundations of Discrete Mathematics
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 CS104 : Discrete Structures Chapter V Graph Theory.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs 2015, Fall Pusan National University Ki-Joune Li.
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
Introduction to Graph Theory By: Arun Kumar (Asst. Professor) (Asst. Professor)
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Data Structures 13th Week
Graph Algorithms Using Depth First Search
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
2018, Fall Pusan National University Ki-Joune Li
2017, Fall Pusan National University Ki-Joune Li
Trees-2, Graphs Data Structures with C Chpater-6 Course code: 10CS35
Presentation transcript:

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

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

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 ≠

Example: Graphs (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

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 (a) Graph with a self edge(b) Multigraph

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

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

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

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.

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.

Example: Path, Simple Path, Cycle 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

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

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.

Example: Connected and Strongly Connected G4G4 H1H1 H2H

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

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.

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.

Example: Biconnected (a)A connected graph 1,3,5,7 is articulation point (b) Its biconnected components

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.

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

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 };

Adjacency Matrix Adjacency Matrix is a two dimensional n×n array. (a) G 1 (b) G

Adjacency Matrix (c) G G4G4 H1H1 H2H2

Adjacency Lists [0] [1] [2] [3] [0] [1] [2] HeadNodes (a) G 1 (b) G

Adjacency Lists [0] [1] [2] [3] HeadNodes (c) G [4] [5] [6] [7] G4G4 H1H1 H2H2

Adjacency Lists : Sequential Representation G4G4 H1H1 H2H2

Inverse Adjacency Lists [0] [1] [2] [0] [1] [2]

Adjacency Lists: Orthogonal List head nodes (shown twice) tail column link for head head row link for tail 0 1 2

Multilists [0] [1] [2] [3] 01N1N3 02N2N3 030N4 12 N 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 list1list

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

Depth-First Search [0] [1] [2] [3] [4] [5] [6] [7] HeadNodes 0,1,3,7,4,5,2,6

Breadth-First Search [0] [1] [2] [3] [4] [5] [6] [7] HeadNodes 0,1,2,3,4,5,6,7

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

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 )

Spanning Trees: Complete Graph

DFS and BFS Spanning Tree (a) DFS (0) spanning tree(b) BFS (0) spanning tree

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 ?

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.

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).

Find Biconnected Components vertex dfn low

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

Kruskal ’ s Algorithm (a)(b)

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;

Prim ’ s Algorithm (a) (b)

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;

Sollin ’ s Algorithm (a) (b)

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

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

Single source/all destinations: Nonnegative edges costs Path Length 1) 0, 3 2) 0, 3, 4 3) 0, 3, 4, 1 4) 0, (a) Graph(b) Shortest paths from 0

Single source/all destinations: Nonnegative edges costs Los Angeles San Francisco Denver New Orleans Mia mi New York Boston Chicago

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

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

Single Source/All Destinations: General Weights (a) A directed graph k dist k [7] ∞∞∞ ∞ (b) dist k

All-Pairs Shortest Paths A ∞0 A0A A1A A2A (b) A -1 (c) A 0 (d) A 1 (e) A 2

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.

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

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.

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.

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.

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

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

Example: Topological Order (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

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

Example: AOE 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

AOE Representation with Adjacency Lists [0] [1] [2] [3] [4] [5] count first vertex 2 [6] 2 [7] 2 0 [8] dur link

Computation Early and Late Activity Time ee[0][1][2][3][4][5][6][7][8]Stack Initial [0] output [3,2,1] output [5,2,1] output [2,1] output [1] output [4] output [7,6] output [6] output [8] output 8