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.

Slides:



Advertisements
Similar presentations
Cpt S 223 – Advanced Data Structures Graph Algorithms: Introduction
Advertisements

Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CHAPTER 6 GRAPHS All the programs in this file are selected from
Review Binary Search Trees Operations on Binary Search Tree
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.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Introduction to Graphs
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
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.
Data Structures Using C++ 2E
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.
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,
Binary Search Trees Berlin Chen 陳柏琳 台灣師範大學資工系 副教授 參考資料來源:
Graphs. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes to each other The set of.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
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.
Data Structures & Algorithms Graphs
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graph Theory and Applications
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.
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
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
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:
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.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Data Structures 13th Week
Csc 2720 Instructor: Zhuojun Duan
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs Chapter 13.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs G = (V, E) V are the vertices; E are the edges.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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:

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 6.2 Elementary Graph Operations 6.3 Minimum Cost Spanning Trees 6.4 Shortest Paths and Transitive Closure 6.5 Activity Networks

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-2 Chapter 6 Graphs Chapter 1 Basic Concepts Chapter 2 Arrays Chapter 3 Stacks and Queues Chapter 4 Linked Lists Chapter 5 Trees Chapter 6 Graph Chapter 7 Sorting Chapter 8 Hashing Chapter 9 Heap Structures Chapter 10 Search Structures Contents

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-3 Chapter 6 Graphs 6.1 The Graph Abstract Data Type structure Graph is objects: a nonempty set of vertices and a set of undirected edges, where each edge is a pair of vertices functions: for all graph  Graph, v, v 1, and v 2  Vertices Graph Create() ::= return an empty graph Graph InsertVertex(graph, v) ::= return a graph with v inserted. Graph InsertEdge(graph, v 1, v 2 ) ::= return a graph with a new edge (v 1, v 2 ). Graph DeleteVertex(graph, v) ::= return a graph with v and all its incident edges removed. Graph DeleteEdge(graph, v 1, v 2 ) ::= return a graph with the edge (v 1, v 2 ) removed. Boolean IsEmpty(graph) ::= if (graph==empty) return TRUE else return FALSE List Adjacent(graph, v) ::= return a list of all vertices adjacent to v. p. 263

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-4 Chapter 6 Graphs Introduction 1736, Euler Koenigberg Bridge Problem: Euler showed: there is a walk starting at any vertex, going through each edge exactly once and terminating at the start vertex iff the degree of each vertex is even called Eulerian walk. the degree of a vertex: # of edges incident to a vertex J. R. Newman: the world of Mathematics, 1956, p C A B D g c d e b f a attaching

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-5 Chapter 6 Graphs A Graph, G = (V, E), consists of two sets V and E V: finite non-empty set of vertices E: set of pairs of vertices, edges, e.g.(1,2) or  1,2  Note: V(G): set of vertices of graph G E(G): set of edges of graph G Undirected Graph – the pair of vertices representing any edge is unordered. Thus, the pairs (V 1,V 2 ) and (V 2,V 1 ) represent the same edge. Directed Graph – each edge is represented by a directed pairs  V 1, V 2  Note:  V 1, V 2  and  V 2, V 1  represent two different edges Definitions V1V1 V2V V(G) = {1, 2, 3} E(G) = {  1,2   2,3   3,1  }

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-6 Chapter 6 Graphs  Examples for Graph G1G1 G2G G3G3 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 )={  0,1 ,  1,0 ,  1,2  } Examples for Graph Note: Graph G2 is also a tree,  Tree is a special case of graph.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-7 Chapter 6 Graphs (a) (b) 02 1 Graph with a self edge Multigraph: multiple occurrences of the same edge Examples for Graphlike Structure Not consider as a graph in this book

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-8 Chapter 6 Graphs A Complete Graph is a graph that has the maximum number of edges For undirected graph with n vertices, the maximum number of edges is n(n-1)/2 For directed graph with n vertices, the maximum number of edges is n(n-1) Example: G1 is a complete graph Complete Graph n(n-1)/2 = 6

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-9 Chapter 6 Graphs If (v0, v1) is an edge in an undirected graph, Adjacent: v0 and v1 are adjacent Incident: The edge (v0, v1) is incident on vertices v0 and v1 If is an edge in a directed graph Adjacent: v0 is adjacent to v1, and v1 is adjacent from v0 Incident: The edge is incident on v0 and v1 Adjacent and Incident G3G The vertices adjacent to vertex 2: 0, 1 and 2 The edge incident on vertex 2: (0,2), (1,2) and (2,3) The vertices adjacent to vertex 1: 0, The edge incident on vertex 1:, and

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-10 Chapter 6 Graphs Subgraph A subgraph of G is a graph G’ → V(G’)  V(G) E(G’)  E(G) e.g. some of the subgraphs of G1: (i) (ii) (iii) (iv) G1G

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-11 Chapter 6 Graphs (i) (ii) (iii) (iv) G3G3 Subgraph (cont.)  e.g. some of the subgraphs of G3: If G is connected with n vertices then its connected subgraph at least has n-1 edges. Why?

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-12 Chapter 6 Graphs Path: from v p to v q in G is a sequence of vertices v p, v i1, v i2,...,v in, v p  (v p, v i1 ), (v i1, v i2 ),..., (v in, v q )  E(G) if G is undirected or  v p, v i1 ,  v i1, v i2 ,...,  v in, v q   E(G) if G is directed eg. Length: The length of a path is the number of edges on it eg. Length of path 1,2,4 is ,2,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Path

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-13 Chapter 6 Graphs Simple Path: is a path in which all vertices except possibly the first and last are distinct. e.g.1. 1,2,4 is a simple path, path: 1,2,4,2 is not a simple path. e.g ,2,3,1 also a simple path with length 3. 1,2,3,1,2? path, length 4, not simple. 1,2,3,1,3 not a path ,2,4 is a path - 1,2,3 is not a path - 1,2,4,2 is a path Simple Path

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-14 Chapter 6 Graphs Cycle Cycle: is a simple path, first and last vertices are same. eg. 1,2,3,1. Note: Directed cycle, Directed path. Acyclic graph G2G2 tree (acyclic graph)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-15 Chapter 6 Graphs Connected Two vertices V 1 and V 2 are Connected: if in an undirected graph G,  a path in G from V 1 to V 2 (or from V 2 to V 1 ∵ undirected)  Strongly Connected V 1, V 2 are Strongly Connected: if in a directed graph (digraph) G’,  a path in G from V 1 to V 2 and also from V 2 to V 1  Graph (Strongly) Connected graph connected (graph strongly connected) if  V i, V j  V(G),  a path from V i to V j in G strongly connected 不是 strongly connected

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-16 Chapter 6 Graphs Connected Component Connected Component (Strongly Connected Component): is a maximal connected subgraph. eg. for graph for digraph Two connected components of G Two strongly connected components of G G2 G1

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-17 Chapter 6 Graphs Degree Degree of Vertex is the # of edges incident to that vertex eg. In Direct Graph If G has n vertices and e edge, d i = degree of V i, then 3 degree of V 3 = 3 in-degree out-degree in-degree=1 out-degree=2 degree=3

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-18 Chapter 6 Graphs Network A graph with weighted edges is called. eg. 台北 花蓮 高雄 台中 新竹 Diagraph G = (V,E) Network N = (G,W) is a diagraph G together with a real valued for w: E  R. R is a real number w(u,v) is the weight of edge (arc) (u,v)  E.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-19 Chapter 6 Graphs  Method 1: Adjacency Matrix  Method 2: Adjacency Lists  Method 3: Adjacency Multilists Graph Representations

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-20 Chapter 6 Graphs Adjacency Matrix Let G = (V, E) with n vertices, n  1. The adjacency matrix of G is a 2-dimensional n  n matrix, A A(i, j) = 1 iff (v i, v j ) (  v i, v j  for a diagraph)  E(G), A(i, j) = 0 otherwise Adjacency Matrix eg degree = d i symmetric - space need n 2 bits, but half can be saved 6 / 2 = 3... edges A

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-21 Chapter 6 Graphs Adjacency Matrix: Examples not be symmetric out-degree = in-degree = d j Eg. 2.

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-22 Chapter 6 Graphs  Examples for Adjacency Matrix G4G4 Adjacency Matrix: Examples

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-23 Chapter 6 Graphs Questions: How many edges e are there in G? Determine the # of edges in G? Is G connected? Answers: Need check entries of the matrix.  check n 2 – n time.  spend O(n 2 ) time to determine e Note: << n 2 /2 (n: Diagonal entries are zero, need not check it) Adjacency Matrix: Determine Edges e = 7 << n 2 /2 = 32

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-24 Chapter 6 Graphs Adjacency Lists Adjacency matrices for G 3 Adjacency list: edge e  2e nodes. Easy random access for any particular vertex List Orthogonal list V0V0 V1V1 V2V2 0  V0V0 V1V1 V2V2 0  Head Nodes (sequential) Adjacency Lists G3G3

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-25 Chapter 6 Graphs [0] [1] [2] [3] An undirected graph with n vertices and e edges  n head nodes and 2e list nodes Adjacency Lists: Example G 1

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-26 Chapter 6 Graphs [0] [1] [2] [3] [4] [5] [6] [7] G4G4 Adjacency Lists: Example G 4

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-27 Chapter 6 Graphs  Number of nodes in a graph: = the number of nodes in adjacency list  Number of edges in a graph = determined in O(n+e)  Out-degree of a vertex in a directed graph = the number of nodes in its adjacency list  In-degree of a vertex in a directed graph: ??? traverse the whole data structure V0V0 V1V1 V2V G3G3 Adjacency Lists: Interesting Operations n vertices# of edges vs. O(n 2 ) in adjacency matrix

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-28 Chapter 6 Graphs  [0] [1] [2] 1 NULL 0 NULL 1 NULL  determine in-degree of a vertex in a fast way  Inverse Adjacency List Adjacency Lists: Inverse Adjacency Lists

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-29 Chapter 6 Graphs tail head column link for head row link for tail Adjacency Lists: Orthogonal Representation  Orthogonal representation Sparse Matrix ??? head node

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-30 Chapter 6 Graphs Adjacency Multilists  Adjacency Multilists ( 以 edge 為主 ) mark vertex1 vertex2 path1 path N1 N2 N3 N4 N5N6 vertex 0: N1  N2  N3 vertex 1: N1  N4  N5 vertex 2: N2  N4  N6 vertex 3: N3  N5  N6 [0] [1] [2] [3] 0 1 N2 N4 0 2 N3 N4 0 3 N5 1 2 N5 N6 1 3 N6 2 3 N1 N2 N3 N4 N5 N6 edge(0,1) edge(0,2) edge(0,3) edge(1,2) edge(1,3) edge(2,3)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-31 Chapter 6 Graphs Adjacency Multilists (cont.) N1 N2 N3 N4 N5N6 vertex 0: N1  N2  N3 vertex 1: N1  N4  N5 vertex 2: N2  N4  N6 vertex 3: N3  N5  N6 mark vertex1 vertex2 Link 1 for V 1 Link 2 for V 2  Node Structures [0] [1] [2] [3] 0 1 N2 N4 0 2 N3 N4 0 3 N5 1 2 N5 N6 1 3 N6 2 3 N1 N2 N3 N4 N5 N6 edge(0,1) edge(0,2) edge(0,3) edge(1,2) edge(1,3) edge(2,3)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-32 Chapter 6 Graphs Adjacency MultiLists vs. Adjacency List Adjacency Multilists ( 以 edge 為主 ) e=6  6 nodes Adjacency List N1 N2 N3 N4 N5N6

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-33 Chapter 6 Graphs Traversal Given G = (V, E) and vertex v, find or visit all w  V, such that w connects v. Method 1: Depth First Search (DFS)  preorder tree traversal Method 2: Breadth First Search (BFS)  level order tree traversal Connected Components (Application 1 of Graph traversal) Spanning Trees (Application 2 of Graph traversal) Minimum Cost Spanning Tree (Application 3 of Graph traversal) … 6.2 Elementary Graph Operations G4G4

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-34 Chapter 6 Graphs Graph Traversal Given: an undirected graph G = (V, E), a vertex v  V(G) Interested in: visiting all vertices connected to v. (reachable) Approach: 1. Depth First Search (DFS). 2. Breadth First Search (BFS) DFS: 1,2,4,5,3,6 BFS: 1,2,3,4,5, stack 123 Queue Graph Operations: Graph Traversal

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-35 Chapter 6 Graphs [0] [1] [2] [3] [4] [5] [6] [7] Example Depth First Search Adjacency Lists

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-36 Chapter 6 Graphs  Example 6.1: Depth first search  v 0, v 1, v 3, v 7, v 4, v 5, v 2, v 6  Program 6.1, p [0] [1] [2] [3] [4] [5] [6] [7] Depth First Search: Example stack

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-37 Chapter 6 Graphs Depth First Search Procedure DFS(v) (* Array VISITED(n)  0 initially and VISITED(i)  1, if has visited*) VISITED(v)  1 print(v) for each vertex w adjacent to v do if VISITED(w) = 0 then call DFS(w) end end DFS Depth First Search: Algorithm VISITED Output  DFS: 1,2,4,8,5,6,3,7 V1V2V3V1V2V3 V2V4V5V2V4V5 V4V8V4V8 V8V5V6V7V8V5V6V7 V5V5 V6V3V6V3 V3V7V3V7 V7V7

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-38 Chapter 6 Graphs Time complexity: Adjacency list Time complexity: Adjacency matrix e edges 2e nodes 1670 Depth First Search: Time Complexity Adjacency matrix: O(n 2 ) n: # of vertex Adjacency list: O(e) e: # of edges

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-39 Chapter 6 Graphs  Example 6.2: Breadth first search  v 0, v 1, v 2, v 3, v 4, v 5, v 6, v 7  Program 6:2, p [0] [1] [2] [3] [4] [5] [6] [7] Breadth First Search

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-40 Chapter 6 Graphs Breadth First Search procedure BFS(v) VISITED (v)  1 print (v) initialize Q with v while Q not =  do call DELETE Q(v,Q) for all vertices w adjacent to v do if VISITED (w)=0 then [ call ADDQ(w,Q); VISITED(w)  1, print(w)] end end end BFS visited V… Breadth First Search: Algorithm Q

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-41 Chapter 6 Graphs eg. V1V1 V2V2 V3V3 V4V4 V5V5 V6V6 V7V7 V8V8 Q V1V1 V2V2 V3V3 V4V4 V5V5 V6V6 V7V7 V8V8 Print: V 1, V 2, V 3, V 4, V 5, V 6, V 7, V 8. Breadth First Search: Example

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-42 Chapter 6 Graphs Time complexity: by using Adjacency Matrix while for n n Breadth First Search: Time Complexity O(n 2 )

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-43 Chapter 6 Graphs Time complexity: by using Adjacency List: Total: d 1 + d d n = O(e) where d i = degree (v i )  while for d i : degree of (v i ) didi n Breadth First Search: Time Complexity O(e)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-44 Chapter 6 Graphs Application 1. Finding components of a graph Application 2. Finding a spanning tree of a connected graph Application 3. Minimum cost spanning tree Applications of Graph Traversal

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-45 Chapter 6 Graphs Procedure COMP(G) (* determine the connected components of G *) for i  1 to n do VISITED(i)  0 for i  1 to n do if VISITED(i) = 0 then call DFS(i); print(“||”) end end COMP Determine connected graph Invoke DFS or BFS Application 1. Finding Components of a Graph Connected Components DFS: || || BFS: || || VISITED 1 1

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-46 Chapter 6 Graphs Application 2. Finding a Spanning Tree of a Connected Graph Def. Application 2.1– obtaining circuit equations for an electrical network Application 2.2 – minimum cost spanning tree Def. Spanning Tree Any tree consisting only edges in G and including all vertices in G is called. i.e. minimal subgraph G’ of G, such that V(G’) =V(G) and G’ is connected Spanning Trees

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-47 Chapter 6 Graphs eg. How to find it? Modify BFS Procedure BFS(v) If VISITTED(w)=0 thenIf VISITTED(w)=0 then 1.call ADDQ(w,Q) 1. 2.VISITTED(w)← print(w) 3. 4.T = T ∪ {(v, w)}...(How many?) Ans : 16 ( EX.) Spanning Trees (cont.)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-48 Chapter 6 Graphs Modify Procedure DFS :同 modify Procedure BFS e.g. Application 1– obtaining circuit equations for an electrical network BFS: 1,2,3, DFS: 1,2,4,3 BF spanning treeDF spanning tree Spanning Trees (cont.)

Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-49 Chapter 6 Graphs BFS Spanning DFS Spanning Tree vs. BFS Spanning Tree DFS Spanning