CSE 373: Data Structures and Algorithms

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
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CSE 373: Data Structures and Algorithms
CHAPTER 13 GRAPH ALGORITHMS ACKNOWLEDGEMENT: THESE SLIDES ARE ADAPTED FROM SLIDES PROVIDED WITH DATA STRUCTURES AND ALGORITHMS IN C++, GOODRICH, TAMASSIA.
Programming Abstractions Cynthia Lee CS106B. Upcoming Topics Graphs! 1.Basics  What are they? How do we represent them? 2.Theorems  What are some things.
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.
Graphs ORD SFO LAX DFW Graphs 1 Graphs Graphs
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs A New Data Structure
CSE 373 Data Structures and Algorithms
Graphs.
Graphs Chapter 20.
Data Structures Graphs - Terminology
CSE 373 Topological Sort Graph Traversals
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Cse 373 May 8th – Dijkstras.
Ellen Walker CPSC 201 Data Structures Hiram College
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS120 Graphs.
CMSC 341 Lecture 21 Graphs (Introduction)
CSE 373: Data Structures and Algorithms
Discrete Maths 9. Graphs Objective
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs CSE 2011 Winter November 2018.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Can you get there from here?
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
CSE 373 Data Structures Lecture 16
CSE 373 Data Structures and Algorithms
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms
Algorithms: Design and Analysis
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Graphs Chapter 7 Visit for more Learning Resources.
Fundamental Data Structures and Algorithms
Graph Implementation.
Depth-First Search CSE 2011 Winter April 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
CSE 373 Data Structures and Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
abstract containers sequence/linear (1 to 1) hierarchical (1 to many)
Graphs.
Graphs.
Graphs.
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
Introduction to Graphs
Presentation transcript:

CSE 373: Data Structures and Algorithms Lecture 19: Graphs

What are graphs? Yes, this is a graph…. But we are interested in a different kind of “graph”

Airline Routes Nodes = cities Edges = direct flights

Computer Networks 56 Tokyo Seattle Seoul 128 16 181 New York 30 140 L.A. Tokyo Sydney Seoul 128 140 181 30 16 56 Nodes = computers Edges = transmission rates

CSE Course Prerequisites at UW 461 322 373 143 321 326 415 142 410 370 341 413 417 378 Nodes = courses Directed edge = prerequisite 421 401

Graphs graph: a data structure containing a set of vertices V a set of edges E, where an edge represents a connection between 2 vertices G = (V, E) edge is a pair (v, w) where v, w in V the graph at right: V = {a, b, c} and E = {(a, b), (b, c), (c, a)} Assuming that a graph can only have one edge between a pair of vertices and cannot have an edge to itself, what is the maximum number of edges a graph can contain, relative to the size of the vertex set V?

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: path from V to Z: {b, h} or {V, X, Z} reachability: v1 is reachable from v2 if a path exists from V1 to V2 connected graph: one in which it's possible to reach any node from any other is this graph connected? P1 X U V W Z Y a c b e d f g h P2

Cycles cycle: path from one node back to itself example: {b, g, f, c, a} or {V, X, Y, W, U, V} loop: edge directly from node to itself many graphs don't allow loops C1 X U V W Z Y a c b e d f g h C2

Weighted graphs weight: (optional) cost associated with a given edge example: graph of airline flights if we were programming this graph, what information would we have to store for each vertex / edge? ORD PVD MIA DFW SFO LAX LGA HNL 849 802 1387 1743 1843 1099 1120 1233 337 2555 142

Directed graphs directed graph (digraph): edges are one-way connections between vertices if graph is directed, a vertex has a separate in/out degree

Trees as Graphs Every tree is a graph with some restrictions: the tree is directed there is exactly one directed path from the root to every node A B C D E F G H

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 X U V W Z Y a c b e d f g h i j

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?

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?

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

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

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

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

Another DFS example Using DFS, find a path from BOS to LAX. v BOS ORD JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6

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

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

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 vi of v where there is an edge from v to vi: mark vi as visited List.addLast(vi). 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)

Another BFS example Using BFS, find a path from BOS to LAX. v BOS ORD JFK BOS MIA ORD LAX DFW SFO v 2 1 3 4 5 6

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? O(|V| + |E|) Space: DFS O(1), BFS O(|V| + |E|)