Copyright 2000-2009 Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.

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
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
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.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Chapter 3 The Greedy Method 3.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
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.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Minimum-Cost Spanning Tree weighted connected undirected graph spanning tree cost of spanning tree is sum of edge costs find spanning tree that has minimum.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
C. Y. Tang and J. S. Roger Jang
Graph Algorithms Using Depth First Search Prepared by John Reif, Ph.D. Distinguished Professor of Computer Science Duke University Analysis of Algorithms.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
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:
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.
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.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Graph Dr. Bernard Chen Ph.D. University of Central Arkansas.
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
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.
6.1.3 Graph representation.
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.
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
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.
Department of Computer Engineering Faculty of Engineering, Prince of Songkla University Graphs Original Slides by Rada Mihalcea.
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.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Data Structures 13th Week
CHAPTER 6 GRAPHS All the programs in this file are selected from
Graph Algorithm.
Minimum-Cost Spanning Tree
Graphs Chapter 13.
6.1.3 Graph representation.
2018, Fall Pusan National University Ki-Joune Li
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
2017, Fall Pusan National University Ki-Joune Li
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Minimum-Cost Spanning Tree
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer Science Press, 2008 Fall 2009 Course, Sungkyunkwan University Hyunseung Choo

Fall 2009 Data Structures The Graph Abstract Data Type Introduction the bridges of Koenigsberg c d e ab f g A Kneiphof C B D c ab d e g f C A B D (a)(b) Networking Laboratory 2/48

Fall 2009 Data Structures The Graph Abstract Data Type Def) A graph G = (V,E) where V(G): nonempty finite set of vertices, E(G): finite set of edges possibly empty  undirected graph: unordered (v i,v j ) = (v j,v i )  directed graph ordered:  G1G G2G G3G3 Networking Laboratory 3/48

Fall 2009 Data Structures The Graph Abstract Data Type restrictions on graphs  no edge from a vertex, i, back to itself (no self loop) - not allowed (v i,v i ) or  no multiple occurrences of the same edge (  multigraph) examples of a graph with feedback loops and a multigraph (a)(b) Networking Laboratory 4/48

Fall 2009 Data Structures The Graph Abstract Data Type complete graph  the maximum number of edges  undirected graph with n vertices max number of edges = n(n-1)/2  directed graph with n vertices max number of edges = n(n-1) adjacent  v i and v j are adjacent if (v i,v j )  E(G) adjacent to(from) for digraphs  : a directed edge  vertex v 0 is adjacent to vertex v 1  vertex v 1 is adjacent from vertex v 0 incident  an edge e = (v i,v j ) is incident on vertices v i and v j Networking Laboratory 5/48

Fall 2009 Data Structures The Graph Abstract Data Type subgraph G’ of G  V(G’)  V(G) and E(G’)  E(G) (i)(ii)(iii)(iv) some of the subgraphs of G G1G1 Networking Laboratory 6/48

Fall 2009 Data Structures The Graph Abstract Data Type path (from vertex v p to vertex v q ): a sequence of vertices, v p,v i1,v i2,···,v in,v q such that (v p,v i1 ), (v i1,v i2 ), ···, (v in,v q ) are edges in an undirected graph or,,···, are edges in a directed graph length of path: # of edges on the path (i) (ii) (iii)(iv) some of the subgraphs of G G3G3 Networking Laboratory 7/48

Fall 2009 Data Structures The Graph Abstract Data Type Simple path: a path in which all vertices, except possibly the first and the last, are distinct Cycle: a path in which the first and last vertices are the same  simple cycle for directed graph: add the prefix “directed” to the terms cycle and path  simple directed path  directed cycle, simple directed cycle Connected  vertex v 0 and v 1 is connected, if there is a path from v 0 to v 1 in an undirected graph G  an undirected graph is connected if, for every pair of vertices v i and v j, there is a path from v i to v j Networking Laboratory 8/48

Fall 2009 Data Structures connected component (of an undirected graph)  maximal connected subgraph a graph with two connected components 0312 H1H G4G4 H2H2 The Graph Abstract Data Type Networking Laboratory 9/48

Fall 2009 Data Structures strongly connected (in a directed graph)  for every pair of vertices v i, v j in V(G) there is a directed path from v i to v j and also from v j to v i strongly connected directed graph strongly connected component  maximal subgraph that is strongly connected strongly connected components of G The Graph Abstract Data Type Networking Laboratory 10/48

Fall 2009 Data Structures degree (of a vertex): number of edges incident to that vertex in-degree (of a vertex v): number of edges that have v as the head (for directed graphs) out-degree (of a vertex v): number of edges that have v as the tail (for directed graphs) special types of graphs  tree: an acyclic connected graph  bipartite graph  planar graph  complete graph The Graph Abstract Data Type Networking Laboratory 11/48

Fall 2009 Data Structures Graph Representations Adjacency matrix  G = (V,E) with |V| = n(  1)  two-dimensional n  n array, say adj_mat[ ][ ]  adj_mat[i][j] =  “1” if (v i,v j ) is adjacent  “0” otherwise  space complexity: S(n) = n 2  symmetric for undirected graphs  asymmetric for directed graphs Networking Laboratory 12/48

Fall 2009 Data Structures adjacency matrices for G 1, G 3, and G 4 G1G1 G3G3 G4G4 Graph Representations Networking Laboratory 13/48

Fall 2009 Data Structures Graph Representations Adjacency lists  replace n rows of adjacency matrix with n linked lists  every vertex i in G has one list #define MAX_VERTICES 50 typedef struct node *node_ptr; typdef struct node { int vertex; node_ptr link; }; node_ptr graph[MAX_VERTICES]; int n = 0; /* vertices currently in use */ vertexlink Networking Laboratory 14/48

Fall 2009 Data Structures headnodevertexlink G1G1 G3G3 Graph Representations Networking Laboratory 15/48

Fall 2009 Data Structures adjacency lists for G 1, G 3, and G G4G4 Graph Representations Networking Laboratory 16/48

Fall 2009 Data Structures Graph Representations Inverse adjacency lists  useful for finding in-degree of a vertex in digraphs  contain one list for each vertex  each list contains a node for each vertex adjacent to the vertex that the list represents inverse adjacency list for G Networking Laboratory 17/48

Fall 2009 Data Structures Orthogonal representation  change the node structure of the adjacency lists orthogonal representation for graph G 3 tailheadcolumn link for headrow link for tail headnodes (shown twice) 1 Graph Representations Networking Laboratory 18/48

Fall 2009 Data Structures vertices may appear in any order alternate order adjacency list for G headnodevertexlink Graph Representations Networking Laboratory 19/48

Fall 2009 Data Structures weighted edges  assign weights to edges of a graph  distance from one vertex to another, or  cost of going from one vertex to an adjacent vertex  modify representation to signify an edge with the weight of the edge  for adj matrix : weight instead of 1  for adj list : weight field network  a graph with weighted edges Graph Representations Networking Laboratory 20/48

Fall 2009 Data Structures graph traversals  visit every vertex in a graph  what order?  DFS(Depth First Search): similar to a preorder tree traversal  BFS(Breath First Search): similar to a level-order tree traversal v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 (a) Elementary Graph Operations Networking Laboratory 21/48

Fall 2009 Data Structures graph G and its adjacency lists (b) Elementary Graph Operations Networking Laboratory 22/48

Fall 2009 Data Structures DFS (depth first search)  easy to implement recursively  stack  a global array visited[MAX_VERTICES]  initialized to FALSE  change visited[i] to TRUE when a vertex i is visited Elementary Graph Operations #define FALSE 0 #define TRUE 1 short int visited[MAX_VERTICES]; Networking Laboratory 23/48

Fall 2009 Data Structures EX) v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v v0v0 v1v1 v0v0 v7v7 v3v3 v1v1 v0v0 v4v4 v7v7 v3v3 v1v1 v0v0 v7v7 v3v3 v1v1 v0v0 visited: Elementary Graph Operations Networking Laboratory 24/48

Fall 2009 Data Structures time complexity for dfs()  time complexity for adj list representation: O(e)  time complexity for adj matrix representation: O(n 2 ) Elementary Graph Operations void dfs(int v) { /* depth first search of a graph beginning with vertex v */ node_ptr w; visited[v] = TRUE; printf(“%5d”, v); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) dfs(w->vertex); } Networking Laboratory 25/48

Fall 2009 Data Structures BFS(breadth first search)  use a dynamically linked queue  each queue node contains vertex and link fields Elementary Graph Operations typedef struct queue *queue_ptr; typedef struct queue { int vertex; queue_ptr link; }; void insert(queue_ptr *, queue_ptr *, int); void delete(queue_ptr *); Networking Laboratory 26/48

Fall 2009 Data Structures v0v0 v1v1 v2v2 v2v2 v3v3 v4v4 v3v3 v4v4 v5v5 v6v6 v4v4 v5v5 v6v6 v7v7 v1v1 v2v2 v3v3 front v5v5 v6v6 v7v7 v4v4 v6v6 v7v7 v5v5 v7v7 v6v6 v7v7 Elementary Graph Operations Networking Laboratory 27/48

Fall 2009 Data Structures time complexity for bfs()  time complexity for adj list: O(e)  time complexity for adj matrix: O(n 2 ) Elementary Graph Operations void bfs(int v) { node_ptr w; queue_ptr front, rear; front = rear = NULL;/* initialize queue */ printf(“%5d”, v); visited[v] = TRUE; insert(&front, &rear, v); while (front) { v = delete(&front); for (w = graph[v]; w; w = w->link) if (!visited[w->vertex]) { printf(“%5d”, w->vertex); add(&front, &rear, w->vertex); visited[w->vertex] = TRUE; } } } Networking Laboratory 28/48

Fall 2009 Data Structures connected components  determine whether or not an undirected graph is connected - simply calling dfs(0) or bfs(0) and then determine if there are unvisited vertices  list the connected components of a graph - make repeated calls to either dfs(v) or bfs(v) where v is an unvisited vertex  time complexity: O(n+e)  total time by dfs: O(e),  for loop: O(n) Elementary Graph Operations void connected(void) { /* determine the connected components of a graph */ int i; for (i = 0; i < n; i++) if (!visited[i]) { dfs(i); printf(“\n”); } Networking Laboratory 29/48

Fall 2009 Data Structures spanning trees when graph G is connected dfs or bfs implicitly partitions the edges in G into two sets:  T(for tree edges): set of edges used or traversed during the search  N(for nontree edges): set of remaining edges  edges in T form a tree that includes all vertices of G Def) A spanning tree is any tree that consists solely of edges in G and that include all the vertices in G Elementary Graph Operations Networking Laboratory 30/48

Fall 2009 Data Structures a complete graph and three spanning trees depth first spanning tree  use dfs to create a spanning tree breadth first spanning tree  use bfs to create a spanning tree Elementary Graph Operations Networking Laboratory 31/48

Fall 2009 Data Structures dfs and bfs spanning trees properties of spanning trees 1) if we add a nontree edge into a spanning tree  cycle 2) spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected 3) |E(G’)| = n - 1 where |V(G)| = n  minimum cost spanning trees v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 (a) dfs(0) spanning tree(b) bfs(0) spanning tree Elementary Graph Operations Networking Laboratory 32/48

Fall 2009 Data Structures biconnected components and articulation points (cut-points) articulation point  a vertex v of G  deletion of v, together with all edges incident on v, produce a graph, G’, that has at least two connected components biconnected graph  a connected graph that has no articulation points v0v0 v1v1 v2v2 v3v3 v4v4 v5v5 v6v6 v7v7 Example of biconnected graph Elementary Graph Operations Networking Laboratory 33/48

Fall 2009 Data Structures  a connected graph which is not biconnected  articulation points are 1,3,5,7  biconnected component  maximal biconnected subgraph, H, of connected undirected graph G  two biconnected components of the same graph have no more than one vertex in common  no edge can be in two or more biconnected components of a graph  biconnected components of a graph G partition the edges of G Elementary Graph Operations Networking Laboratory 34/48

Fall 2009 Data Structures a connected graph and its biconnected components (a) connected graph (b) biconnected components Elementary Graph Operations Networking Laboratory 35/48

Fall 2009 Data Structures depth first number, or dfn  sequence in which the vertices are visited during the depth first search  if u is an ancestor of v in the df spanning tree, dfn(u) < dfn(v) (a) depth first spanning tree dfs(3) (b) Elementary Graph Operations Networking Laboratory 36/48

Fall 2009 Data Structures cost of a spanning tree of a weighted undirected graph  sum of the costs(weights) of the edges in the spanning tree  a spanning tree of least cost  Kruskal’s, Prim’s, and Sollin’s algorithms  greedy method: construct an optimal solution in stages, make the best decision at each stage using some criterion for spanning tree: least cost criterion  use only edges within the graph  use exactly n-1 edges  may not use edges that would produce a cycle Minimum Cost Spanning Trees Networking Laboratory 37/48

Fall 2009 Data Structures (1) Kruskal’s algorithm  select the edges for inclusion in T in nondecreasing order of their cost  an edge is added to T if it does not form a cycle with the edges that are already in T  exactly n-1 edges are selected Minimum Cost Spanning Trees Networking Laboratory 38/48

Fall 2009 Data Structures Kruskal’s algorithm Minimum Cost Spanning Trees T = {}; while (T contains less than n-1 edges && E is not empty) { choose a least cost edge (v,w) from E; delete (v,w) from E; if ((v,w) does not create a cycle in T) add (v,w) to T; else discard (v,w); } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory 39/48

Fall 2009 Data Structures choosing a least cost edge(v,w) from E  a min heap  determine and delete the next least cost edge: O(log 2 e)  construction of the heap: O(e) checking that the new edge,(v,w), does not form a cycle in T  use the union-find operations Minimum Cost Spanning Trees Networking Laboratory 40/48

Fall 2009 Data Structures (a) (b) (c) stages in Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 41/48

Fall 2009 Data Structures (d) (e) (f) stages in Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 42/48

Fall 2009 Data Structures (g) (h) Minimum Cost Spanning Trees stages in Kruskal’s algorithm Networking Laboratory 43/48

Fall 2009 Data Structures summary of the Kruskal’s algorithm Minimum Cost Spanning Trees Networking Laboratory 44/48

Fall 2009 Data Structures (2) Prim’s algorithm  begins with a tree, T, that contains a single vertex  add a least cost edge (u,v) to T such that T  {(u,v)} is also a tree and repeat this step until T contains n-1 edges  set of selected edges at each stage of the algorithm  form a tree in Prim’s alg.  form a forest in Kruskal’s alg. Minimum Cost Spanning Trees Networking Laboratory 45/48

Fall 2009 Data Structures Prim’s algorithm Minimum Cost Spanning Trees T = {}; TV = {0}; /* start with vertex 0 and no edge*/ while (T contains fewer than n-1 edges) { let (u,v) be a least cost edge such that u  TV and v  TV; if (there is no such edge) break; add v to TV; add (u,v) to T; } if (T contains fewer than n-1 edges) printf(“no spanning tree\n”); Networking Laboratory 46/48

Fall 2009 Data Structures (a)(b)(c) stages in Prim’s algorithm Minimum Cost Spanning Trees Networking Laboratory 47/48

Fall 2009 Data Structures Minimum Cost Spanning Trees (d)(e)(f) stages in Prim’s algorithm Networking Laboratory 48/48