Graph Search Lecture 17 CS 2110 Spring 2018.

Slides:



Advertisements
Similar presentations
GRAPHS Lecture 18 CS2110 – Fall Graph Algorithms 2 Search –depth-first search –breadth-first search Shortest paths –Dijkstra's algorithm Minimum.
Advertisements

CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs - II CS 2110, Spring Where did I leave that book?
Graph Searching CSE 373 Data Structures Lecture 20.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
ITEC200 – Week 12 Graphs. 2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Alyce Brady CS 510: Computer Algorithms Depth-First Graph Traversal Algorithm.
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.
GRAPHS Lecture 19 CS2110 – Fall Time to do A4, Recursion Histogram: max: [00:02): 17 av: 5.2 [02:04): 102 median: 4.5 [04:06): 134 [06:08):
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.
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. What is a graph? In simple words, A graph is a set of vertices and edges which connect them. A node (or vertex) is a discrete position in the.
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,
Graphs - II CS 2110, Spring Where did David leave that book? 2.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
CSC 172 DATA STRUCTURES.
CSE 373 Data Structures and Algorithms
Graphs Lecture 19 CS2110 – Spring 2013.
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
Graph Traversals Some algorithms require that every vertex of a graph be visited exactly once. The order in which the vertices are visited may be important,
1 Spanning Trees Lecture 22 CS2110 – Spring 2017.
Graph Algorithms BFS, DFS, Dijkstra’s.
Searching Graphs ORD SFO LAX DFW Spring 2007
CSE 2331/5331 Topic 9: Basic Graph Alg.
Graph Search Lecture 17 CS 2110 Fall 2017.
Lecture 12 Graph Algorithms
Ellen Walker CPSC 201 Data Structures Hiram College
CC 215 Data Structures Graph Searching
Spanning Trees, greedy algorithms
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
Unweighted Shortest Path Neil Tang 3/11/2010
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Data Structures and Algorithms for Information Processing
Short paths and spanning trees
CSC 172 DATA STRUCTURES.
Graph Algorithm.
Graphs Representation, BFS, DFS
Graphs Lecture 18 CS2110 – Fall 2009.
Graphs Chapter 13.
Depth-First Search D B A C E Depth-First Search Depth-First Search
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Lecture 13 CSE 331 Sep 27, 2017.
Graphs – Adjacency Matrix
CSE 373 Data Structures Lecture 16
Spanning Trees Longin Jan Latecki Temple University based on slides by
Depth-First Search D B A C E Depth-First Search Depth-First Search
Searching Graphs ORD SFO LAX DFW Spring 2007
Lecture 14 CSE 331 Oct 3, 2012.
COMP171 Depth-First Search.
Algorithms: Design and Analysis
Breadth-First Search L0 L1 L2 C B A E D F Breadth-First Search
Weighted Graphs & Shortest Paths
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
A vertex u is reachable from vertex v iff there is a path from v to u.
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Spanning Trees, greedy algorithms
Lecture 10 Graph Algorithms
Lecture 11 Graph Algorithms
Lecture 13 CSE 331 Sep 28, 2016.
Presentation transcript:

Graph Search Lecture 17 CS 2110 Spring 2018

Announcements A5 due tonight A6 is out, remember to get started early For the next  lecture, you MUST watch the tutorial on the shortest path algorithm beforehand:     http://www.cs.cornell.edu/courses/cs2110/2017f a/online/shortestPath/shortestPath.html The class on 4/10 will assume that you understand it. Watch the tutorial once or twice and execute the algorithm on a small graph. Complete Quiz 4 by 4/9

Graphs

Representing Graphs 1 2 3 4 Adjacency List Adjacency Matrix 1 2 3 4 2 1 2 3 4 1 2 3 4 2 3 4 1 Demo 1: show implementations of ListGraph and MatrixGraph in Java 0 1 0 1 0 0 1 0 0 0 0 0 0 1 1 0

Graph Algorithms Search Depth-first search Breadth-first search Shortest paths Dijkstra's algorithm Spanning trees Algorithms based on properties Minimum spanning trees Prim's algorithm Kruskal's algorithm

Search on Graphs Given a graph 𝑉,𝐸 and a vertex 𝑢∈𝑉 We want to "visit" each node that is reachable from 𝑢 1 7 2 5 3 4 6 8 There are many paths to some nodes. How do we visit all nodes efficiently, without doing extra work? 1 7 2 5 3 4 6 8

Depth-First Search Intuition: Recursively visit all vertices that are reachable along unvisited paths. /** Visit all nodes reachable on unvisited paths from u. Precondition: u is unvisited. */ public static void dfs(int u) { visit(u); for all edges (u,v): if(!visited[v]): dfs(v); } 1 7 2 5 3 4 6 8 3 2 1 Demo 2: after walk through + order, code this up 7 5 8 dfs(1) visits the nodes in this order: 1, 2, 3, 5, 7, 8

Depth-First Search Intuition: Recursively visit all vertices that are reachable along unvisited paths. /** Visit all nodes reachable on unvisited paths from u. Precondition: u is unvisited. */ public static void dfs(int u) { visit(u); for all edges (u,v): if(!visited(v)): dfs(v); } Suppose there are 𝑛 vertices that are reachable along unvisited paths and 𝑚 edges: Worst-case running time? 𝑂(𝑛+𝑚) Worst-case space? 𝑂(𝑛)

DFS Quiz In what order would a DFS visit the vertices of this graph? Break ties by visiting the lower- numbered vertex first. 1, 2, 3, 4, 5, 6, 7, 8 1, 2, 5, 6, 3, 6, 7, 4, 7, 8 1, 2, 5, 3, 6, 4, 7, 8 1, 2, 5, 6, 3, 7, 4, 8 1 7 2 5 3 4 6 8

Depth-First Search in Java public class Node { boolean visited; List<Node> neighbors; /** Visit all nodes reachable on unvisited paths from this node. Precondition: this node is unvisited. */ public void dfs() { visited= true; for (Node n: neighbors) { if (!n.visited) n.dfs(); } Each vertex of the graph is an object of type Node No need for a parameter. The object is the node.

Depth-First Search Iteratively Intuition: Visit all vertices that are reachable along unvisited paths from the current node. /** Visit all nodes reachable on unvisited paths from u. Precondition: u is unvisited. */ public static void dfs(int u) { Stack s= (u);// Not Java! while ( ) { u= s.pop(); if (u not visited) { visit u; for each edge (u, v): s.push(v); } 1 7 2 5 3 4 6 8 3 2 1 s is not empty 7 5 8 7 8 5 Stack: 1 2 3

Breadth-First Search /** Visit all nodes reachable on unvisited paths from u. Precondition: u is unvisited. */ public static void bfs(int u) { Queue q= (u);// Not Java! while ( q is not empty ) { u= q.remove(); if (u not visited) { visit u; for each (u, v): q.add(v); } Intuition: Iteratively process the graph in "layers" moving further away from the source node. 1 7 2 5 3 4 6 8 3 2 1 7 5 Demo 3: before this slide, copy-paste code DFSIterative into BFS and change to queue 8 Queue: 2 1 5 7 3 5 8 5

Analyzing BFS /** Visit all nodes reachable on unvisited paths from u. Precondition: u is unvisited. */ public static void bfs(int u) { Queue q= (u);// Not Java! while ( ) { u= q.remove(); if (u not visited) { visit u; for each (u, v): q.add(v); } Intuition: Iteratively process the graph in "layers" moving further away from the source node. Suppose there are 𝑛 vertices that are reachable along unvisited paths and 𝑚 edges: Worst-case running time? 𝑂(𝑛+𝑚) Worst-case space? 𝑂(𝑚)

BFS Quiz In what order would a BFS visit the vertices of this graph? Break ties by visiting the lower- numbered vertex first. 1, 2, 3, 4, 5, 6, 7, 8 1, 2, 3, 4, 5, 6, 6, 7, 7, 8 1, 2, 5, 3, 6, 4, 7, 8 1, 2, 5, 6, 3, 7, 4, 8 1 7 2 5 3 4 6 8

Comparing Search Algorithms DFS BFS Visits: 1, 2, 3, 5, 7, 8 Time: 𝑂(𝑛+𝑚) Space: 𝑂(𝑛) Visits: 1, 2, 5, 7, 3, 8 Time: 𝑂(𝑛+𝑚) Space: 𝑂(𝑚) 1 7 2 5 3 4 6 8 Other factors: completeness