CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search

Slides:



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

Graphs and Digraphs Chapter 14.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graph Searching CSE 373 Data Structures Lecture 20.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
Graphs. 2 Some Examples and Terminology A graph is a set of vertices (nodes) and a set of edges (arcs) such that each edge is associated with exactly.
Graph Search Methods A vertex u is reachable from vertex v iff there is a path from v to u
Graphs Chapter 30 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
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)
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.
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.
Graphs Chapter 30 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
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.
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.
Graphs. Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
1 Data Structures and Algorithms Graphs I: Representation and Search Gal A. Kaminka Computer Science Department.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
1 Graphs Algorithms Sections 9.1, 9.2, and Graphs v1v1 v2v2 v5v5 v7v7 v8v8 v3v3 v6v6 v4v4 A graph G = (V, E) –V: set of vertices (nodes) –E: set.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
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,
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
CSE 373: Data Structures and Algorithms
Programming Abstractions Cynthia Lee CS106X. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
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.
Graphs Chapter Chapter Contents Some Examples and Terminology Road Maps Airline Routes Mazes Course Prerequisites Trees Traversals Breadth-First.
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 28 © 2015 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures and Abstractions with Java, 4e Frank Carrano.
Graphs and Paths : Chapter 15 Saurav Karmakar
Discrete Maths 11. Graph Theory Objective
Chapter 05 Introduction to Graph And Search Algorithms.
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
Graphs David Kauchak cs302 Spring Admin HW 12 and 13 (and likely 14) You can submit revised solutions to any problem you missed Also submit your.
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
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CMSC 341 Lecture 21 Graphs (Introduction)
Discrete Maths 9. Graphs Objective
Graphs Chapter 11 Objectives Upon completion you will be able to:
CSE 373: Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Graphs Chapter 7 Visit for more Learning Resources.
Presentation transcript:

CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search reading: Weiss Ch. 9 slides created by Marty Stepp http://www.cs.washington.edu/373/ © University of Washington, all rights reserved.

What is a graph? 56 Tokyo Seattle Seoul 128 16 181 New York 30 140 L.A. Tokyo Sydney Seoul 128 140 181 30 16 56

Graphs graph: A data structure containing: the graph at right: a set of vertices V, (sometimes called nodes) a set of edges E, where an edge represents a connection between 2 vertices. Graph G = (V, E) an edge is a pair (v, w) where v, w are in V the graph at right: V = {a, b, c, d} E = {(a, c), (b, c), (b, d), (c, d)} degree: number of edges touching a given vertex. at right: a=1, b=2, c=3, d=2 a c b d

Graph examples For each, what are the vertices and what are the edges? Web pages with links Methods in a program that call each other Road maps (e.g., Google maps) Airline routes Facebook friends Course pre-requisites Family trees Paths through a maze

Paths path: A path from vertex a to b is a sequence of edges that can be followed starting from a to reach b. can be represented as vertices visited, or edges taken example, one path from V to Z: {b, h} or {V, X, Z} What are two paths from U to Y? path length: Number of vertices or edges contained in the path. neighbor or adjacent: Two vertices connected directly by an edge. example: V and X X U V W Z Y a c b e d f g h

Reachability, connectedness reachable: Vertex a is reachable from b if a path exists from a to b. connected: A graph is connected if every vertex is reachable from any other. Is the graph at top right connected? strongly connected: When every vertex has an edge to every other vertex. X U V W Z Y a c b e d f g h a c b d e a b c d

Loops and cycles cycle: A path that begins and ends at the same node. example: {b, g, f, c, a} or {V, X, Y, W, U, V}. example: {c, d, a} or {U, W, V, U}. acyclic graph: One that does not contain any cycles. loop: An edge directly from a node to itself. Many graphs don't allow loops. X U V W Z Y a c b e d f g h

Weighted graphs weight: Cost associated with a given edge. Some graphs have weighted edges, and some are unweighted. Edges in an unweighted graph can be thought of as having equal weight (e.g. all 0, or all 1, etc.) Most graphs do not allow negative weights. example: graph of airline flights, weighted by miles between cities: ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

Directed graphs directed graph ("digraph"): One where edges are one-way connections between vertices. If graph is directed, a vertex has a separate in/out degree. A digraph can be weighted or unweighted. Is the graph below connected? Why or why not? a d b e g f c

Digraph example Vertices = UW CSE courses (incomplete list) Edge (a, b) = a is a prerequisite for b 120 131 142 140 450 143 154 311 331 341 351 373 374 332 344 403 312 333 352 410 413 414 431 446 444 421 440 451 415 417 452

Linked Lists, Trees, Graphs A binary tree is a graph with some restrictions: The tree is an unweighted, directed, acyclic graph (DAG). Each node's in-degree is at most 1, and out-degree is at most 2. There is exactly one path from the root to every node. A linked list is also a graph: Unweighted DAG. In/out degree of at most 1 for all nodes. F B K A B C D A E H G J

Searching for paths Searching for a path from one vertex to another: Sometimes, we just want any path (or want to know there is a path). Sometimes, we want to minimize path length (# of edges). Sometimes, we want to minimize path cost (sum of edge weights). What is the shortest path from MIA to SFO? Which path has the minimum cost? ORD PVD MIA DFW SFO LAX LGA HNL $50 $80 $140 $170 $70 $100 $110 $120 $60 $250 $200 $500 $130

Depth-first search depth-first search (DFS): Finds a path between two vertices by exploring each possible path as far as possible before backtracking. Often implemented recursively. Many graph algorithms involve visiting or marking vertices. Depth-first paths from a to all vertices (assuming ABC edge order): to b: {a, b} to c: {a, b, e, f, c} to d: {a, d} to e: {a, b, e} to f: {a, b, e, f} to g: {a, d, g} to h: {a, d, g, h} a e b c h g d f

DFS pseudocode function dfs(v1, v2): dfs(v1, v2, { }). function dfs(v1, v2, path): path += v1. mark v1 as visited. if v1 is v2: a path is found! for each unvisited neighbor n of v1: if dfs(n, v2, path) finds a path: a path is found! path -= v1. // path is not found. The path param above is used if you want to have the path available as a list once you are done. Trace dfs(a, f) in the above graph. a e b c h g d f

DFS observations discovery: DFS is guaranteed to find a path if one exists. retrieval: It is easy to retrieve exactly what the path is (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, f) returns {a, d, c, f} rather than {a, d, f}. a e b c h g d f

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 queue of vertices to visit. BFS always returns the shortest path (the one with the fewest edges) between the start and the end vertices. to b: {a, b} to c: {a, e, f, c} to d: {a, d} to e: {a, e} to f: {a, e, f} to g: {a, d, g} to h: {a, d, h} a e b c h g d f

BFS pseudocode Trace bfs(a, f) in the above graph. function bfs(v1, v2): queue := {v1}. mark v1 as visited. while queue is not empty: v := queue.removeFirst(). if v is v2: a path is found! for each unvisited neighbor n of v: mark n as visited. queue.addLast(n). // path is not found. Trace bfs(a, f) in the above graph. a e b c h g d f

BFS observations optimality: always finds the shortest path (fewest edges). in unweighted graphs, finds optimal cost path. In weighted graphs, not always optimal cost. retrieval: harder to reconstruct the actual sequence of vertices or edges in the path 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 solution: We can keep track of the path by storing predecessors for each vertex (each vertex can store a reference to a previous vertex). DFS uses less memory than BFS, easier to reconstruct the path once found; but DFS does not always find shortest path. BFS does. a e b c h g d f

DFS, BFS runtime What is the expected runtime of DFS and BFS, in terms of the number of vertices V and the number of edges E ? Answer: O(|V| + |E|) where |V| = number of vertices, |E| = number of edges Must potentially visit every node and/or examine every edge once. why not O(|V| * |E|) ? What is the space complexity of each algorithm? (How much memory does each algorithm require?)

BFS that finds path function bfs(v1, v2): queue := {v1}. mark v1 as visited. while queue is not empty: v := queue.removeFirst(). if v is v2: a path is found! (reconstruct it by following .prev back to v1.) for each unvisited neighbor n of v: mark n as visited. (set n.prev = v.) queue.addLast(n). // path is not found. By storing some kind of "previous" reference associated with each vertex, you can reconstruct your path back once you find v2. a e b c h g d f prev