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 / 07 / 2008 Instructor: Michael Eckmann.
Shortest Paths and Dijkstra's Algorithm CS 110: Data Structures and Algorithms First Semester,
CSE 373: Data Structures and Algorithms Lecture 18: Graphs II 1.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
Depth-First Search Lecture 24 COMP171 Fall Graph / Slide 2 Depth-First Search (DFS) * DFS is another popular graph search strategy n Idea is similar.
Dijkstras Algorithm Named after its discoverer, Dutch computer scientist Edsger Dijkstra, is an algorithm that solves the single-source shortest path problem.
Minimum-Cost Spanning Tree CS 110: Data Structures and Algorithms First Semester,
Shortest Paths and Dijkstra’s Algorithm CS 105. SSSP Slide 2 Single-source shortest paths Given a weighted graph G and a source vertex v in G, determine.
Breadth First Search Maedeh Mehravaran Big data 1394.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
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.
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs – Breadth First Search
Minimum-cost spanning tree
Minimum-Cost Spanning Tree and Kruskal’s Algorithm
Thinking about Algorithms Abstractly
A vertex u is reachable from vertex v iff there is a path from v to u.
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
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,
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.
Graph Search Lecture 17 CS 2110 Fall 2017.
CS 106B Homework 7: Trailblazer
Artificial Intelligence (CS 370D)
CC 215 Data Structures Graph Searching
Depth-First Search.
Unweighted Shortest Path Neil Tang 3/11/2010
Instructor: Lilian de Greef Quarter: Summer 2017
Data Structures and Algorithms for Information Processing
CMSC 341 Lecture 21 Graphs (Introduction)
CSE 373: Data Structures and Algorithms
Graphs.
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Can you get there from here?
Chapter 22: Elementary Graph Algorithms I
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 and Algorithms
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Graphs Part 2 Adjacency Matrix
Tree Searching.
Lecture 13 CSE 331 Sep 27, 2017.
Depth-First Search Graph Traversals Depth-First Search DFS.
CSE 373 Data Structures Lecture 16
Single-source shortest paths
COMP171 Depth-First Search.
CSE 373: Data Structures and Algorithms
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.
Algorithms: Design and Analysis
Graphs Chapter 7 Visit for more Learning Resources.
Fundamental Data Structures and Algorithms
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Depth-First Search CSE 2011 Winter April 2019.
CSE 373 Data Structures and Algorithms
A vertex u is reachable from vertex v iff there is a path from v to u.
CSE 373 Graphs 3: Implementation reading: Weiss Ch. 9
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
Lecture 13 CSE 331 Sep 28, 2016.
Presentation transcript:

CSE 373 Data Structures and Algorithms Lecture 21: Graphs III

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 alphabetical edge order) A A  B 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.

VertexInfo class public class VertexInfo<V> { public V v; public boolean visited; public VertexInfo(V v) { this.v = v; clear(); } public void clear() { visited = false;

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 alphabetical edge order) A A  B 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 In terms of the number of vertices |V| and the number of edges |E|: What is the expected runtime of DFS? What is the expected runtime of BFS? Answer: O(|V| + |E|) Each algorithm must potentially visit every node and/or examine every edge once. What is the space complexity of each algorithm? O(|V|) O(|V| + |E|) Space: DFS O(1), BFS O(|V| + |E|)