bfs for single source shortest path
play

BFS for Single Source Shortest Path Dr. Mattox Beckman University - PowerPoint PPT Presentation

Introduction BFS for Single Source Shortest Path Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction Objectives Your Objectives: Implement SSSP using BFS Introduction The Algorithm


  1. Introduction BFS for Single Source Shortest Path Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science

  2. Introduction Objectives Your Objectives: ◮ Implement SSSP using BFS

  3. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist a f Parent c

  4. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 ∞ ∞ ∞ ∞ ∞ ∞ a f Parent - - - - - - - c

  5. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 1 1 1 ∞ ∞ ∞ a f Parent - a a a - - - c

  6. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 1 1 1 2 ∞ ∞ a f Parent - a a a b - - c

  7. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 1 1 1 2 2 ∞ a f Parent - a a a b c - c

  8. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 1 1 1 2 2 ∞ a f Parent - a a a b c - c

  9. Introduction The Algorithm e g ◮ Use this if your graph is unweighted b ◮ Create a distance array and a parent array d a b c d e f g Dist 0 1 1 1 2 2 3 a f Parent - a a a b c e c

  10. int u = q.front(); q.pop(); dist[v.first] = dist[u] + 1; for ( int j = 0; j < ( int )AdjList[u].size(); j ++ ) { ii v = AdjList[u][j]; q.push(v.first); if (dist[v.first] == INF) { parent[v.first] = u; Introduction Implementation 0 // Credit: Competitive Programming 3 1 vi dist (V, INF); dist[s] = 0; 2 queue <int> q; q.push(s); 3 vi parent; 4 while ( ! q.empty()) { 5 6 7 8 9 10 11 12 } } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend