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

Slides:



Advertisements
Similar presentations
Algorithms (and Datastructures) Lecture 3 MAS 714 part 2 Hartmut Klauck.
Advertisements

Graphs CSE 331 Section 2 James Daly. Reminders Homework 4 is out Due Thursday in class Project 3 is out Covers graphs (discussed today and Thursday) Due.
Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CSE 373 Graphs 1: Concepts, Depth/Breadth-First Search
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graph Searching CSE 373 Data Structures Lecture 20.
CSE 373: Data Structures and Algorithms Lecture 19: Graphs III 1.
Breadth-First and Depth-First Search
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.
CS 206 Introduction to Computer Science II 11 / 07 / 2008 Instructor: Michael Eckmann.
Graphs COL 106 Slide Courtesy : Douglas W. Harder, U Waterloo.
CSCI 3160 Design and Analysis of Algorithms Tutorial 2 Chengyu Lin.
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.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
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.
CS 206 Introduction to Computer Science II 11 / 10 / 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 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Shortest Path Problem For weighted graphs it is often useful to find the shortest path between two vertices Here, the “shortest path” is the path that.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Dijkstra’s Algorithm Slide Courtesy: Uwash, UT 1.
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.
Computer Science 112 Fundamentals of Programming II Graph Algorithms.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
1 Chapter 9 Graph Algorithms Real-life graph problems Algorithms for some graph problems Choice of data structures for graph problems.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
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,
Data Structures and Algorithms Ver. 1.0 Session 17 Objectives In this session, you will learn to: Implement a graph Apply graphs to solve programming problems.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
Graphs Upon completion you will be able to:
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.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
CS 367 Introduction to Data Structures Lecture 13.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
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 Lecture 21: Graphs V 1.
CSE 373 Data Structures and Algorithms
Graphs Representation, BFS, DFS
CS120 Graphs.
Comp 245 Data Structures Graphs.
Graphs Representation, BFS, DFS
"Learning how to learn is life's most important skill. " - Tony Buzan
CSE 373: Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 11 Graphs.
Shortest Path Algorithms
Directed Graph Algorithms
Directed Graph Algorithms
Richard Anderson Autumn 2016 Lecture 5
CSE 373: Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CSE 373 Data Structures and Algorithms
CE 221 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 5
Presentation transcript:

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

2 A Graph: Course Prerequisites Nodes = courses Directed edge = prerequisite

Depth-First Search (DFS) depth-first search (DFS): find path between two vertices by exploring each path as many steps as possible before backtracking – often implemented recursively with a stack for the path in progress – always finds a path, but not necessarily the shortest one – easy to reconstruct the path once you have found it (just unroll the calls) DFS path search order 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 3

DFS pseudocode dfs(v1, v2): path = new Stack(). dfs(v1, v2, path) dfs(v1, v2, path): path.push(v1). mark v1 as visited. if v1 = v2: path is found. for each unvisited neighbor v i of v1 with edge (v1  v i ): if dfs(v i, v2, path) finds a path, path is found. path.pop(). // path is not found. 4

Breadth-First Search (BFS) breadth-first search (DFS): find path between two nodes by taking one step down all paths and then immediately backtracking – often implemented with a queue of next vertices to visit – always finds the shortest path (fewest edges); optimal for unweighted graphs – harder to reconstruct the path once you have found it BFS path search order from A to others: – A – A  B – A  C – A  E – A  B  D – A  B  F – A  C  G 5

BFS pseudocode bfs(v1, v2): Q = {v1}. mark v1 as visited. while Q not empty: v = Q.dequeue(). // remove from front if v is v2: path is found. for each unvisited neighbor v i of v1 with edge (v1  v i ): mark v i as visited. Q.enqueue(v i ). // add at end path is not found. 6

Implementation: Adjacency Matrix an n × n 2D array where M[a][b] = edges from v a to v b – Sometimes implemented as a Map > – Good for quickly asking, "is there an edge from vertex i to j?" – How do we figure out the degree of a vertex?

Implementation: Adjacency Lists n lists of neighbors; L[a] = all edges out from v a – Sometimes implemented as a Map > – Good for processing all-neighbors, sparse graphs (few edges) – How do we figure out the degree of a vertex?

Dijkstra's algorithm Dijkstra's algorithm: finds shortest (min weight) path between a pair of vertices in a weighted directed graph with nonnegative edges – solves the "one vertex, shortest path" problem – basic algorithm concept: create a table of information about the currently known best way to reach each vertex (distance, previous vertex) and improve it until it reaches the best solution 9 A GF B ECD

Dijkstra pseudocode Dijkstra(v1, v2): for each vertex v: // Initialize state v's distance := infinity. v's previous := none. v1's distance := 0. Q := {all vertices}. while Q is not empty: v := remove Q's vertex with min distance. mark v as known. for each unknown neighbor n of v: dist := v's distance + edge (v, n)'s weight. if dist is smaller than n's distance: n's distance := dist. n's previous := v. reconstruct path from v2 back to v1, following previous pointers. 10 A GF B ECD      examine A: update B(2),D(1) examine D: update C(3),E(3),F(9),G(5) examine B,E: update none examine C: update F(8) examine G: update F(6) examine F: update none

Floyd-Warshall algorithm Floyd-Warshall algorithm: finds shortest (min weight) path between all pairs of vertices in a weighted directed graph – solves the "all pairs, shortest paths" problem(demo)demo – idea: repeatedly find best path using only vertices 1..k inclusive floydWarshall(): int path[n][n]. for each (i, j) from (0, 0) to (n, n): path[i][j] = edge_weight[i][j]. for k = 0 to n: for i = 0 to n: for j = 0 to n: path[i][j] = min(path[i][j], path[i][k] + path[k][j]). 11

Topological Sort Topological sort: finds a total ordering of vertices such that for any edge (v, w) in E, v precedes w in the ordering – e.g. find an ordering in which all UW CSE courses can be taken 12 A B C D E F FAED BC

Topological Sort pseudocode V = {all vertices}. E = {all edges}. L = []. while V is not empty: for each vertex v in V: if v has no incoming edges: V.remove(v). L.append(v). for each edge e (v  n): E.remove(e). return L. examine A,D examine B examine C examine E examine F B C D E A F