Graph & BFS Lecture 22 COMP171 Fall 2006. Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

CSE 373, Copyright S. Tanimoto, 2001 Graphs Graphs 2 Incidence and Adjacency Representing a graph with an adjacency matrix, an incidence matrix,
Graph Theory, DFS & BFS Kelly Choi What is a graph? A set of vertices and edges –Directed/Undirected –Weighted/Unweighted –Cyclic/Acyclic.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
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.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Structure and Algorithms (BCS 1223) GRAPH. Introduction of Graph A graph G consists of two things: 1.A set V of elements called nodes(or points or.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Graph & BFS.
Graphs Motivation and Terminology Representations Traversals Variety of Problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Connected Components, Directed Graphs, Topological Sort COMP171.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Introduction to Graphs
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
COMP171 Depth-First Search.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Connected Components, Directed Graphs, Topological Sort Lecture 25 COMP171 Fall 2006.
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.
Depth-first search COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar to pre-order.
CS 206 Introduction to Computer Science II 11 / 09 / 2009 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
 Last lesson  Graphs  Today  Graphs (Implementation, Traversal)
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Social Media Mining Graph Essentials.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Chapter 2 Graph Algorithms.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs CSE 2011 Winter June Graphs A graph is a pair (V, E), where  V is a set of nodes, called vertices  E is a collection of pairs.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Most of contents are provided by the website Graph Essentials TJTSD66: Advanced Topics in Social Media.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Data Structures CSCI 132, Spring 2014 Lecture 38 Graphs
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
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 Slide credits:  K. Wayne, Princeton U.  C. E. Leiserson and E. Demaine, MIT  K. Birman, Cornell U.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
Discrete Structures CISC 2315 FALL 2010 Graphs & Trees.
Chapter 9: Graphs.
Chapter 05 Introduction to Graph And Search Algorithms.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
Graphs A New Data Structure
Graph & BFS.
Graphs Representation, BFS, DFS
Depth-First Search.
CMSC 341 Lecture 21 Graphs (Introduction)
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs CSE 2011 Winter November 2018.
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.
Graphs G = (V, E) V are the vertices; E are the edges.
Introduction to Graphs
Presentation transcript:

Graph & BFS Lecture 22 COMP171 Fall 2006

Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge Vertices can be considered “sites” or locations. Edges represent connections.

Graph & BFS / Slide 3 Application 1 Air flight system Each vertex represents a city Each edge represents a direct flight between two cities A query on direct flights = a query on whether an edge exists A query on how to get to a location = does a path exist from A to B We can even associate costs to edges (weighted graphs), then ask “what is the cheapest path from A to B”

Graph & BFS / Slide 4 Application 2 Wireless communication * Represented by a weighted complete graph (every two vertices are connected by an edge) * Each edge represents the Euclidean distance dij between two stations * Each station uses a certain power i to transmit messages. Given this power i, only a few nodes can be reached (bold edges). A station reachable by i then uses its own power to relay the message to other stations not reachable by i. * A typical wireless communication problem is: how to broadcast between all stations such that they are all connected and the power consumption is minimized.

Graph & BFS / Slide 5 Definition * A graph G=(V, E) consists a set of vertices, V, and a set of edges, E. * Each edge is a pair of (v, w), where v, w belongs to V * If the pair is unordered, the graph is undirected; otherwise it is directed {c,f} {a,c}{a,b} {b,d} {c,d} {e,f} {b,e} An undirected graph

Graph & BFS / Slide 6 Terminology 1. If v 1 and v 2 are connected, they are said to be adjacent vertices  v 1 and v 2 are endpoints of the edge {v 1, v 2 } 2. If an edge e is connected to v, then v is said to be incident on e. Also, the edge e is said to be incident on v. 3. {v 1, v 2 } = {v 2, v 1 } If we are talking about directed graphs, where edges have direction. This means that {v 1,v 2 } ≠ {v 2,v 1 }. Directed graphs are drawn with arrows (called arcs) between edges. AB This means {A,B} only, not {B,A}

Graph & BFS / Slide 7 Graph Representation * Two popular computer representations of a graph. Both represent the vertex set and the edge set, but in different ways. 1. Adjacency Matrix Use a 2D matrix to represent the graph 2. Adjacency List Use a 1D array of linked lists

Graph & BFS / Slide 8 Adjacency Matrix * 2D array A[0..n-1, 0..n-1], where n is the number of vertices in the graph * Each row and column is indexed by the vertex id n e,g a=0, b=1, c=2, d=3, e=4 * A[i][j]=1 if there is an edge connecting vertices i and j; otherwise, A[i][j]=0 * The storage requirement is Θ(n 2 ). It is not efficient if the graph has few edges. An adjacency matrix is an appropriate representation if the graph is dense: |E|=Θ(|V| 2 ) * We can detect in O(1) time whether two vertices are connected.

Graph & BFS / Slide 9 Adjacency List * If the graph is not dense, in other words, sparse, a better solution is an adjacency list * The adjacency list is an array A[0..n-1] of lists, where n is the number of vertices in the graph. * Each array entry is indexed by the vertex id * Each list A[i] stores the ids of the vertices adjacent to vertex i

Graph & BFS / Slide 10 Adjacency Matrix Example

Graph & BFS / Slide 11 Adjacency List Example

Graph & BFS / Slide 12 * The array takes up Θ(n) space * Define degree of v, deg(v), to be the number of edges incident to v. Then, the total space to store the graph is proportional to: * An edge e={u,v} of the graph contributes a count of 1 to deg(u) and contributes a count 1 to deg(v) * Therefore, Σ vertex v deg(v) = 2m, where m is the total number of edges * In all, the adjacency list takes up Θ(n+m) space n If m = O(n 2 ) (i.e. dense graphs), both adjacent matrix and adjacent lists use Θ(n 2 ) space. n If m = O(n), adjacent list outperform adjacent matrix * However, one cannot tell in O(1) time whether two vertices are connected Storage of Adjacency List

Graph & BFS / Slide 13 Adjacency List vs. Matrix * Adjacency List n More compact than adjacency matrices if graph has few edges n Requires more time to find if an edge exists * Adjacency Matrix n Always require n 2 space  This can waste a lot of space if the number of edges are sparse n Can quickly find if an edge exists

Graph & BFS / Slide 14 Path between Vertices * A path is a sequence of vertices (v 0, v 1, v 2,… v k ) such that: n For 0 ≤ i < k, {v i, v i+1 } is an edge Note: a path is allowed to go through the same vertex or the same edge any number of times! * The length of a path is the number of edges on the path

Graph & BFS / Slide 15 Types of paths * A path is simple if and only if it does not contain a vertex more than once. * A path is a cycle if and only if v 0 = v k  The beginning and end are the same vertex! * A path contains a cycle as its sub-path if some vertex appears twice or more

Graph & BFS / Slide 16 Path Examples 1.{a,c,f,e} 2.{a,b,d,c,f,e} 3.{a, c, d, b, d, c, f, e} 4.{a,c,d,b,a} 5.{a,c,f,e,b,d,c,a} Are these paths? Any cycles? What is the path’s length?

Graph & BFS / Slide 17 Graph Traversal * Application example n Given a graph representation and a vertex s in the graph n Find all paths from s to other vertices * Two common graph traversal algorithms  Breadth-First Search (BFS) l Find the shortest paths in an unweighted graph  Depth-First Search (DFS) l Topological sort l Find strongly connected components

Graph & BFS / Slide 18 BFS and Shortest Path Problem * Given any source vertex s, BFS visits the other vertices at increasing distances away from s. In doing so, BFS discovers paths from s to other vertices * What do we mean by “distance”? The number of edges on a path from s Consider s=vertex 1 Nodes at distance 1? 2, 3, 7, s Example Nodes at distance 2? 8, 6, 5, 4 Nodes at distance 3? 0

Graph & BFS / Slide 19 BFS Algorithm Why use queue? Need FIFO // flag[ ]: visited table

Graph & BFS / Slide 20 BFS Example Adjacency List source Visited Table (T/F) F F F F F F F F F F Q = { } Initialize visited table (all False) Initialize Q to be empty

Graph & BFS / Slide Adjacency List source Visited Table (T/F) F F T F F F F F F F Q = { 2 } Flag that 2 has been visited Place source 2 on the queue

Graph & BFS / Slide Adjacency List source Visited Table (T/F) F T T F T F F F T F Q = {2} → { 8, 1, 4 } Mark neighbors as visited 1, 4, 8 Dequeue 2. Place all unvisited neighbors of 2 on the queue Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T F T F F F T T Q = { 8, 1, 4 } → { 1, 4, 0, 9 } Mark new visited Neighbors 0, 9 Dequeue Place all unvisited neighbors of 8 on the queue. -- Notice that 2 is not placed on the queue again, it has been visited! Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 1, 4, 0, 9 } → { 4, 0, 9, 3, 7 } Mark new visited Neighbors 3, 7 Dequeue Place all unvisited neighbors of 1 on the queue. -- Only nodes 3 and 7 haven’t been visited yet. Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 4, 0, 9, 3, 7 } → { 0, 9, 3, 7 } Dequeue has no unvisited neighbors! Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 0, 9, 3, 7 } → { 9, 3, 7 } Dequeue has no unvisited neighbors! Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T F F T T T Q = { 9, 3, 7 } → { 3, 7 } Dequeue has no unvisited neighbors! Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T T F T T T Q = { 3, 7 } → { 7, 5 } Dequeue place neighbor 5 on the queue. Neighbors Mark new visited Vertex 5

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 7, 5 } → { 5, 6 } Dequeue place neighbor 6 on the queue Neighbors Mark new visited Vertex 6

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 5, 6} → { 6 } Dequeue no unvisited neighbors of 5 Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { 6 } → { } Dequeue no unvisited neighbors of 6 Neighbors

Graph & BFS / Slide Adjacency List source Visited Table (T/F) T T T T T T T T T T Q = { } STOP!!! Q is empty!!! What did we discover? Look at “visited” tables. There exists a path from source vertex 2 to all vertices in the graph

Graph & BFS / Slide 33 Time Complexity of BFS (Using Adjacency List) * Assume adjacency list n n = number of vertices m = number of edges Each vertex will enter Q at most once. Each iteration takes time proportional to deg(v) + 1 (the number 1 is to account for the case where deg(v) = the work required is 1, not 0). O(n + m)

Graph & BFS / Slide 34 Running Time * Recall: Given a graph with m edges, what is the total degree? * The total running time of the while loop is: this is summing over all the iterations in the while loop! O( Σ vertex v (deg(v) + 1) ) = O(n+m) Σ vertex v deg(v) = 2m

Graph & BFS / Slide 35 Time Complexity of BFS (Using Adjacency Matrix) * Assume adjacency list n n = number of vertices m = number of edges Finding the adjacent vertices of v requires checking all elements in the row. This takes linear time O(n). Summing over all the n iterations, the total running time is O(n 2 ). O(n 2 ) So, with adjacency matrix, BFS is O(n 2 ) independent of the number of edges m. With adjacent lists, BFS is O(n+m); if m=O(n 2 ) like in a dense graph, O(n+m)=O(n 2 ).