Edmonds Karp Algorithm Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

edmonds karp algorithm
SMART_READER_LITE
LIVE PREVIEW

Edmonds Karp Algorithm Dr. Mattox Beckman University of Illinois at - - PowerPoint PPT Presentation

Introduction Edmonds Karp Algorithm Dr. Mattox Beckman University of Illinois at Urbana-Champaign Department of Computer Science Introduction Objectives Your Objectives: Implement the Edmonds Karp algorithm for Network Flow Introduction


slide-1
SLIDE 1

Introduction

Edmonds Karp Algorithm

  • Dr. Mattox Beckman

University of Illinois at Urbana-Champaign Department of Computer Science

slide-2
SLIDE 2

Introduction

Objectives

Your Objectives: ◮ Implement the Edmonds Karp algorithm for Network Flow

slide-3
SLIDE 3

Introduction

A simple example

A B C D 10/0 5/0 5/0 5/0 10/0

slide-4
SLIDE 4

Introduction

A simple example

A B C D 10/0 5/0 5/0 5/0 10/0

slide-5
SLIDE 5

Introduction

A simple example

A B C D 10/5 5/0 5/0 5/5 10/0

slide-6
SLIDE 6

Introduction

A simple example

A B C D 10/5 5/0 5/0 5/5 10/0

slide-7
SLIDE 7

Introduction

A simple example

A B C D 10/5 5/5 5/0 5/5 10/5

slide-8
SLIDE 8

Introduction

A simple example

A B C D 10/5 5/5 5/0 5/5 10/5

slide-9
SLIDE 9

Introduction

A simple example

A B C D 10/10 5/5 5/5 5/5 10/10

slide-10
SLIDE 10

Introduction

A second example

A B C D 5/0 10/0 5/0 10/0 5/0

slide-11
SLIDE 11

Introduction

A second example

A B C D 5/0 10/0 5/0 10/0 5/0

slide-12
SLIDE 12

Introduction

A second example

A B C D 5/5 10/0 5/5 10/0 5/5

slide-13
SLIDE 13

Introduction

A second example

A B C D 5/5 10/0 5/5 10/0 5/5

slide-14
SLIDE 14

Introduction

A second example

A B C D 5/5 10/5 5/0 10/5 5/5

slide-15
SLIDE 15

Introduction

Implementation

0 // Stolen from Competitive Programming 3 1 // global variables 2 int res[MAX_V][MAX_V], mf, f, s, t; 3 vi p; // p stores the BFS spanning tree from s 4 5 // traverse BFS spanning tree from s->t 6 void augment(int v, int minEdge) { 7

if (v == s) {

8

f = minEdge;

9

return;

10

} else if (p[v] != -1) {

11

augment(p[v], min(minEdge, res[p[v]][v]));

12

res[p[v]][v] -= f;

13

res[v][p[v]] += f;

14 } }

slide-16
SLIDE 16

Introduction

Implementation, 2

0 mf = 0; 1 while (1) {// O(VE^2) (actually O(V^3 E) Edmonds Karp’s algorithm 2

f = 0;

3

vi dist(MAX_V, INF); dist[s] = 0; queue<int> q; q.push(s);

4

p.assign(MAX_V, -1);

5

while (!q.empty()) {

6

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

7

if (u == t) break; // stop when we reach sink t

8

for (int v = 0; v < MAX_V; v++)

9

if (res[u][v] > 0 && dist[v] == INF)

10

dist[v] = dist[u] + 1, q.push(v), p[v] = u; }

11

augment(t, INF);

12

if (f == 0) break; // we cannot send any more flow

13

mf += f;

14 }