CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.

Slides:



Advertisements
Similar presentations
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Advertisements

CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
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.
CSE 2331/5331 Topic 11: Basic Graph Alg. Representations Undirected graph Directed graph Topological sort.
Graph Searching CSE 373 Data Structures Lecture 20.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Chapter 8, Part I Graph Algorithms.
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Graph Search Methods Spring 2007 CSE, POSTECH. Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u. A search method.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
A Introduction to Computing II Lecture 15: Searching Graphs II Fall Session 2000.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Tyler Robison Summer
CSE332: Data Abstractions Lecture 16: Topological Sort / Graph Traversals Dan Grossman Spring 2010.
Graph & BFS.
Directed Graph Algorithms CSE 373 Data Structures Lecture 14.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
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,
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Spring 2015 Lecture 10: Elementary Graph Algorithms
CSC 331: Algorithm Analysis Decompositions of Graphs.
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,
Introduction to Graphs. Introduction Graphs are a generalization of trees –Nodes or verticies –Edges or arcs Two kinds of graphs –Directed –Undirected.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CSE 373: Data Structures and Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Topological Sort: Definition
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
CSE 421 Algorithms Richard Anderson Winter 2009 Lecture 5.
Chapter 05 Introduction to Graph And Search Algorithms.
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
CS 367 Introduction to Data Structures Lecture 13.
CSE373: Data Structures & Algorithms Lecture 14: Topological Sort / Graph Traversals Dan Grossman Fall 2013.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
CSE 373 Data Structures and Algorithms
Topological Sorting.
CSE 373 Topological Sort Graph Traversals
Depth-First Search.
CSE 373 Data Structures and Algorithms
Topological Sort CSE 373 Data Structures Lecture 19.
CSE 373: Data Structures and Algorithms
Directed Graph Algorithms
Directed Graph Algorithms
CSE 373 Graphs 4: Topological Sort reading: Weiss Ch. 9
Richard Anderson Autumn 2016 Lecture 5
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
CSE 373: Data Structures and Algorithms
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1

Directed graphs directed graph (digraph): edges are one-way connections between vertices 2

3 Trees as Graphs Every tree is a graph with some restrictions: – the tree is directed – the tree is acyclic – there is exactly one directed path from the root to every node A B DE C F HG

More terminology degree: number of edges touching a vertex – example: W has degree 4 – what is the degree of X? of Z? adjacent vertices: connected directly by an edge If graph is directed, a vertex has a separate in/out degree XU V W Z Y a c b e d f g h i j 4

Graph questions Are the following graphs directed or not directed? – Buddy graphs of instant messaging programs? (vertices = users, edges = user being on another's buddy list) – bus line graph depicting all of Seattle's bus stations and routes – graph of movies in which actors have appeared together Are these graphs potentially cyclic? Why or why not? 5

Graph exercise Consider a graph of instant messenger buddies. – What do the vertices represent? What does an edge represent? – Is this graph directed or undirected? Weighted or unweighted? – What does a vertex's degree mean? In degree? Out degree? – Can the graph contain loops? cycles? Consider this graph data: – Jessica's buddy list: Meghan, Alan, Martin. – Meghan's buddy list: Alan, Lori. – Toni's buddy list: Lori, Meghan. – Martin's buddy list: Lori, Meghan. – Alan's buddy list: Martin, Jessica. – Lori's buddy list: Meghan. – Compute the in/out degree of each vertex. Is the graph connected? – Who is the most popular? Least? Who is the most antisocial? – If we're having a party and want to distribute the message the most quickly, who should we tell first? 6

7 Topological Sort Problem: Find an order in which all these courses can be taken. Example: 142  143  378  370  321  341  322  326  421  401 In order to take a course, you must take all of its prerequisites first 142

8 Given a digraph G = (V, E), find a total ordering of its vertices such that: for any edge (v, w) in E, v precedes w in the ordering A B C F D E Topological Sort

9 A B C F D E EADF BC Any total ordering in which all the arrows go to the right is a valid solution Topo sort - good example Note that F can go anywhere in this list because it is not connected. Also the solution is not unique.

10 A B C F D E DAEF BC Any ordering in which an arrow goes to the left is not a valid solution Topo sort - bad example NO!

11 Only acyclic graphs can be topologically sorted A directed graph with a cycle cannot be topologically sorted. A B C F D E

12 Step 1: Identify vertices that have no incoming edges The “in-degree” of these vertices is zero A B C F D E Topological sort algorithm: 1

13 Step 1: Identify vertices that have no incoming edges If no such vertices, graph has only cycle(s) (cyclic graph) Topological sort not possible – Halt. A B C D Example of a cyclic graph Topo sort algorithm: 1a

14 Step 1: Identify vertices that have no incoming edges Select one such vertex A B C F D E Select Topo sort algorithm:1b

15 A B C F D E Step 2: Delete this vertex of in-degree 0 and all its outgoing edges from the graph. Place it in the output. Topo sort algorithm: 2 A

16 Repeat Step 1 and Step 2 until graph is empty A B C F D E Select Continue until done

17 A B C F D E B Select B. Copy to sorted list. Delete B and its edges. B

18 A C F D E BC Select C. Copy to sorted list. Delete C and its edges. C

19 A F D E BC D Select D. Copy to sorted list. Delete D and its edges. D

20 A F E BC D EF Select E. Copy to sorted list. Delete E and its edges. Select F. Copy to sorted list. Delete F and its edges. E, F

21 ABC D EF Done A B C F D E

22 Topological Sort Algorithm 1.Store each vertex’s In-Degree in an array D 2.Initialize queue with all “in-degree=0” vertices 3.While there are vertices remaining in the queue: (a) Dequeue and output a vertex (b) Reduce In-Degree of all vertices adjacent to it by 1 (c) Enqueue any of these vertices whose In-Degree became zero 4.If all vertices are output then success, otherwise there is a cycle.

23 Pseudocode Queue Q := [Vertices with in-degree 0] while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x];// y gets a linked list of vertices while y  null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhile

Queue (before): Queue (after): 1, 6 Answer: Topo Sort w/ queue

Queue (before): 1, 6 Queue (after): 6, 2 Answer: 1 Topo Sort w/ queue

Queue (before): 6, 2 Queue (after): 2 Answer: 1, 6 Topo Sort w/ queue

Queue (before): 2 Queue (after): 3 Answer: 1, 6, 2 Topo Sort w/ queue

Queue (before): 3 Queue (after): 4 Answer: 1, 6, 2, 3 Topo Sort w/ queue

Queue (before): 4 Queue (after): 5 Answer: 1, 6, 2, 3, 4 Topo Sort w/ queue

Queue (before): 5 Queue (after): Answer: 1, 6, 2, 3, 4, 5 Topo Sort w/ queue

Stack (before): Stack (after): 1, 6 Answer: 7 8 Topo Sort w/ stack 1 1

Stack (before): 1, 6 Stack (after): 1, 7, 8 Answer: Topo Sort w/ stack

Stack (before): 1, 7, 8 Stack (after): 1, 7 Answer: 6, Topo Sort w/ stack

Stack (before): 1, 7 Stack (after): 1 Answer: 6, 8, Topo Sort w/ stack

Stack (before): 1 Stack (after): 2 Answer: 6, 8, 7, Topo Sort w/ stack

Stack (before): 2 Stack (after): 3 Answer: 6, 8, 7, 1, Topo Sort w/ stack

Stack (before): 3 Stack (after): 4 Answer: 6, 8, 7, 1, 2, Topo Sort w/ stack

Stack (before): 4 Stack (after): 5 Answer: 6, 8, 7, 1, 2, 3, Topo Sort w/ stack

Stack (before): 5 Stack (after): Answer: 6, 8, 7, 1, 2, 3, 4, Topo Sort w/ stack

Queue (before): Queue (after): 1 Answer: TopoSort Fails (cycle)

Queue (before): 1 Queue (after): 2 Answer: 1 TopoSort Fails (cycle)

Queue (before): 2 Queue (after): Answer: 1, 2 TopoSort Fails (cycle)

43 What is the run-time??? Initialize D // Mapping of vertex to its in-degree Queue Q := [Vertices with in-degree 0] while notEmpty(Q) do x := Dequeue(Q) Output(x) y := A[x];// y gets a linked list of vertices while y  null do D[y.value] := D[y.value] – 1; if D[y.value] = 0 then Enqueue(Q,y.value); y := y.next; endwhile

44 Topological Sort Analysis Initialize In-Degree array: O(|V| + |E|) Initialize Queue with In-Degree 0 vertices: O(|V|) Dequeue and output vertex: – |V| vertices, each takes only O(1) to dequeue and output: O(|V|) Reduce In-Degree of all vertices adjacent to a vertex and Enqueue any In-Degree 0 vertices: – O(|E|) For input graph G=(V,E) run time = O(|V| + |E|) – Linear time!

Depth-first search depth-first search (DFS): finds a path between two vertices by exploring each possible path as many steps as possible before backtracking – often implemented recursively 45

DFS example All DFS paths from A to others (assumes ABC edge order) – A – A -> B – A -> B -> D – A -> B -> F – A -> B -> F -> E – A -> C – A -> C -> G What are the paths that DFS did not find? 46

DFS pseudocode Pseudo-code for depth-first search: dfs(v1, v2): dfs(v1, v2, {}) dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: path is found. for each unvisited neighbor v i of v1 where there is an edge from v1 to v i : if dfs(v i, v2, path) finds a path, path is found. path -= v1. path is not found. 47

DFS observations guaranteed to find a path if one exists easy to retrieve exactly what the path is (to remember the sequence of edges taken) if we find it optimality: not optimal. DFS is guaranteed to find a path, not necessarily the best/shortest path – Example: DFS(A, E) may return A -> B -> F -> E 48

Another DFS example Using DFS, find a path from BOS to LAX. 49

Breadth-first search breadth-first search (BFS): finds a path between two nodes by taking one step down all paths and then immediately backtracking – often implemented by maintaining a list or queue of vertices to visit – BFS always returns the path with the fewest edges between the start and the goal vertices 50

BFS example All BFS paths from A to others (assumes ABC edge order) – A – A -> B – A -> C – A -> E – A -> B -> D – A -> B -> F – A -> C -> G What are the paths that BFS did not find? 51

52 BFS pseudocode Pseudo-code for breadth-first search: bfs(v1, v2): List := {v1}. mark v1 as visited. while List not empty: v := List.removeFirst(). if v is v2: path is found. for each unvisited neighbor v i of v where there is an edge from v to v i : List.addLast(v i ). path is not found.

BFS observations optimality: – in unweighted graphs, optimal. (fewest edges = best) – In weighted graphs, not optimal. (path with fewest edges might not have the lowest weight) disadvantage: harder to reconstruct what the actual path is once you find it – conceptually, BFS is exploring many possible paths in parallel, so it's not easy to store a Path array/list in progress observation: any particular vertex is only part of one partial path at a time – We can keep track of the path by storing predecessors for each vertex (references to the previous vertex in that path) 53

Another BFS example Using BFS, find a path from BOS to SFO. 54

DFS, BFS runtime What is the expected runtime of DFS, in terms of the number of vertices V and the number of edges E ? What is the expected runtime of BFS, in terms of the number of vertices V and the number of edges E ? Answer: O(|V| + |E|) – each algorithm must potentially visit every node and/or examine every edge once. – why not O(|V| * |E|) ? What is the space complexity of each algorithm? 55