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

bfs for single source shortest path
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Introduction

BFS for Single Source Shortest Path

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Introduction

Objectives

Your Objectives: ◮ Implement SSSP using BFS

slide-3
SLIDE 3

Introduction

The Algorithm

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

slide-4
SLIDE 4

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist ∞ ∞ ∞ ∞ ∞ ∞ Parent

  • a

b c d e f g

slide-5
SLIDE 5

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist 1 1 1 ∞ ∞ ∞ Parent

  • a

a a

  • a

b c d e f g

slide-6
SLIDE 6

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist 1 1 1 2 ∞ ∞ Parent

  • a

a a b

  • a

b c d e f g

slide-7
SLIDE 7

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist 1 1 1 2 2 ∞ Parent

  • a

a a b c

  • a

b c d e f g

slide-8
SLIDE 8

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist 1 1 1 2 2 ∞ Parent

  • a

a a b c

  • a

b c d e f g

slide-9
SLIDE 9

Introduction

The Algorithm

◮ Use this if your graph is unweighted ◮ Create a distance array and a parent array a b c d e f g Dist 1 1 1 2 2 3 Parent

  • a

a a b c e a b c d e f g

slide-10
SLIDE 10

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

int u = q.front(); q.pop();

6

for (int j = 0; j < (int)AdjList[u].size(); j++) {

7

ii v = AdjList[u][j];

8

if (dist[v.first] == INF) {

9

dist[v.first] = dist[u] + 1;

10

parent[v.first] = u;

11

q.push(v.first);

12 } } }