6.006- Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS 22.2-22.3.

Slides:



Advertisements
Similar presentations
Introduction to Algorithms Lecture 11 Prof. Costis Daskalakis CLRS , B.4.
Advertisements

Implementing a Graph Implement a graph in three ways: –Adjacency List –Adjacency-Matrix –Pointers/memory for each node (actually a form of adjacency list)
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture10.
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Prof. Swarat Chaudhuri COMP 482: Design and Analysis of Algorithms Spring 2013 Lecture 4.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
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.
Graphs By JJ Shepherd. Introduction Graphs are simply trees with looser restrictions – You can have cycles Historically hard to deal with in computers.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Graphs-Part II Depth First Search (DFS). We Already Covered Breadth First Search(BFS) Traverses the graph one level at a time – Visit all outgoing edges.
Data Structures & Algorithms Graph Search Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
UMass Lowell Computer Science Analysis of Algorithms Prof. Karen Daniels Spring, 2001 Makeup Lecture Chapter 23: Graph Algorithms Depth-First SearchBreadth-First.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
COMP171 Depth-First Search.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
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.
CS261 Data Structures DFS and BFS – Edge List Representation.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
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.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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
Union By Rank Ackermann’s Function Graph Algorithms Rajee S Ramanikanthan Kavya Reddy Musani.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Graphs + Shortest Paths David Kauchak cs302 Spring 2013.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
GRAPH ALGORITHM. Graph A pair G = (V,E) – V = set of vertices (node) – E = set of edges (pairs of vertices) V = (1,2,3,4,5,6,7) E = ( (1,2),(2,3),(3,5),(1,4),(4,5),(6,7)
CSC 213 – Large Scale Programming Lecture 31: Graph Traversals.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Code: BCA302 Data Structures with C Prof. (Dr.) Monalisa Banerjee By.
Introduction to Algorithms
Graphs – Breadth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Thinking about Algorithms Abstractly
CSC317 Graph algorithms Why bother?
CSE 421: Introduction to Algorithms
Graphs Representation, BFS, DFS
Graphs Chapter 11 Objectives Upon completion you will be able to:
Chapter 22: Elementary Graph Algorithms I
Basic Graph Algorithms
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
3.2 Graph Traversal.
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS

Graphs G=(V,E) V a set of vertices  Usually number denoted by n E  V  V a set of edges (pairs of vertices)  Usually number denoted by m  Note m ≤ n(n-1) = O(n 2 ) Flavors:  Pay attention to order of vertices in edge: directed graph  Ignore order: undirected graph Then only n(n-1)/2 possible edges

Examples Undirected V={a,b,c,d} E={{a,b}, {a,c}, {b,c}, {b,d}, {c,d}} Directed V = {a,b,c} E = {(a,c), (a,b) (b,c), (c,b)} a b c d a b c

Pocket Cube 2  2  2 Rubik’s cube Configurations are adjacent, if one can be obtained from the other by quarter turns Basic Question: is solved state reachable with such moves from the starting state?

Representation To solve graph problems, must examine graph So need to represent in computer Four representations with pros/cons  Adjacency lists (of neighbors of each vertex)  Incidence lists (of edges from each vertex)  Adjacency matrix (of which pairs are adjacent)  Implicit representation (as neighbor function)

Example a b c C b / b / c / a c b

Searching Graph We want to get from current Rubik state to “solved” state How do we explore?

Breadth First Search Start with vertex v List all its neighbors (distance 1) Then all their neighbors (distance 2) Etc.

Depth First Search Like exploring a maze From current vertex, move to another Until you get stuck Then backtrack till you find a new place to explore

Problem: Cycles What happens if unknowingly revisit a vertex? BFS: get wrong notion of distance DFS: may get in circles Solution: mark vertices  BFS: if you’ve seen it before, ignore  DFS: if you’ve seen it before, back up

Breadth First Search (BFS)

Outline Initial vertex s  Level 0 For i=1,… grow level i  Find all neighbors of level i-1 vertices  (except those already seen)  i.e. level i contains vertices reachable via a path of i edges and no fewer Level 1 Level 2 Level 3 s

v Example a c vx z sd f s a x z d c f

Outline Initial vertex s  Level 0 For i=1,… grow level i  Find all neighbors of level i-1  (except those already seen)  i.e. level i contains vertices reachable via a path of i edges and no fewer Where can the other edges of the graph be?  Only between nodes in same or adjacent levels Level 1 Level 2 Level 3 s

v Example a c vx z sd f s a x z d c f

Algorithm BFS(V,Adj,s) level={s: 0}; parent = {s: None}; i=1 frontier=[s] #previous level, i-1 while frontier next=[] #next level, i for u in frontier for v in Adj[u] if v not in level #not yet seen level[v] = i #level of u+1 parent[v] = u next.append(v) frontier = next i += 1

Analysis: Runtime Vertex v appears at the frontier at most once  Since then it has a level  And nodes with a level aren’t added again  Total time spent adding nodes to frontier O(n) Adj[v] only scanned once  Just when v is in frontier  Total time  v || Adj[v] || This sum counts each “outgoing” edge So O(m) time spend scanning adjacency lists Total: O(m+n) time --- “Linear time”

Analysis: Correctness Claim: If there is a path of L edges from s to v, then v is added to next when i=L or before Proof: induction  Base case: s is added before setting i=1  Path of length L from s to v  path of length L-1 from s to u, and edge (u,v)  By induction, add u when i=L-1 or before  If v has not already been inserted in next before i=L, it gets added when scan u at i=L  So it happens when i=L or before i.e. why are all nodes reachable from s explored?

Shortest Paths From correctness analysis, conclude more:  Level[v] is length of shortest s—v path Parent pointers form a shortest paths tree  Which is union of shortest paths to all vertices To find shortest path, follow parent pointers  Will end up at s

Depth First Search (DFS)

Outline Explore a maze  Follow path until you get stuck  Backtrack along breadcrumbs till find new exit  i.e. recursively explore

Algorithm parent = {s: None} call DFS-visit (V, Adj, s) Routine DFS-visit (V, Adj, u) for v in Adj[u] if v not in parent #not yet seen parent[v] = u DFS-visit (V, Adj, v) #recurse!

Demo (from s) s 1 (in tree) 2 (in tree) 3 (in tree) 4 (back edge) 5 (forward edge) a b c s a b c d 7 (cross edge) 6 (in tree) d

Runtime Analysis Quite similar to BFS DFS-visit only called once per vertex v  Since next time v is in parent set Edge list of v scanned only once (in that call) So time in DFS-visit is 1/vertex + 1/edge So time is O(n+m)

Correctness? Trickier than BFS Can use induction on length of shortest path from starting vertex  Induction Hypothesis: “each vertex at distance k is visited”  Induction Step: Suppose vertex v at distance k Then some u at distance k-1 with edge (u,v) u is visited (by induction hypothesis) Every edge out of u is checked If v wasn’t previously visited, it gets visited from u

Edge Classification Tree edge used to get to new child Back edge leads from node to ancestor in tree Forward edge leads to descendant in tree Cross edge leads to a different subtree To label what edge is of what type, keep global time counter and store interval during which vertex is on recursion stack Cross edge Forward edge Back edge tree edge

Tradeoffs Solving Rubik’s cube?  BFS gives shortest solution Robot exploring a building?  Robot can trace out the exploration path  Just drops markers behind Only difference is “next vertex” choice  BFS uses a queue  DFS uses a stack (recursion)