+ All Categories
Home > Documents > 8 Graphs 4

8 Graphs 4

Date post: 17-Jul-2016
Category:
Upload: vijayshinde
View: 22 times
Download: 1 times
Share this document with a friend
98
QEEE DSA05 DATA STRUCTURES AND ALGORITHMS G VENKATESH AND MADHAVAN MUKUND LECTURE 8, 2 SEPTEMBER 2014
Transcript
Page 1: 8 Graphs 4

QEEE DSA05 DATA STRUCTURES ANDALGORITHMSG VENKATESH AND MADHAVAN MUKUND LECTURE 8, 2 SEPTEMBER 2014

Page 2: 8 Graphs 4

Tasks with constraintsFor a foreign trip you need to

Get a passport

Buy a ticket

Get a visa

Buy travel insurance

Buy foreign exchange

Buy gifts for your hosts

Page 3: 8 Graphs 4

Tasks with constraintsThere are constraints

Without a passport, you cannot buy a ticket or travel insurance

You need a ticket and insurance for the visa

You need the visa for foreign exchange

You don’t want to invest in gifts unless the trip is confirmed

Page 4: 8 Graphs 4

Goal

Find a sequence in which to complete the tasks, respecting the constraints

Page 5: 8 Graphs 4

Model using graphs

Vertices are tasks

Edge from Task1 to Task2 if Task1 must come before Task2

Getting a passport must precede buying a ticket

Getting a visa must precede buying foreign exchange

Page 6: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy gifts

Page 7: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsOrder of tasks should respect dependencies

Page 8: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsOrder of tasks should respect dependencies

Passport, Ticket, Insurance, Visa, Gift, Forex

Page 9: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsOrder of tasks should respect dependencies

Passport, Ticket, Insurance, Visa, Gift, Forex

Passport, Insurance, Ticket, Visa, Forex, Gift

Page 10: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsOrder of tasks should respect dependencies

Passport, Ticket, Insurance, Visa, Gift, Forex

Passport, Insurance, Ticket, Visa, Forex, Gift

Passport, Ticket, Insurance, Visa, Forex, Gift

Page 11: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsOrder of tasks should respect dependencies

Passport, Ticket, Insurance, Visa, Gift, Forex

Passport, Insurance, Ticket, Visa, Forex, Gift

Passport, Ticket, Insurance, Visa, Forex, Gift

Passport, Insurance, Ticket, Visa, Gift, Forex

Page 12: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy gifts

Page 13: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsFeatures of the graph

Page 14: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsFeatures of the graph

Directed

Page 15: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsFeatures of the graph

Directed

No cycles

Page 16: 8 Graphs 4

Our example as a graph

Get passport

Buy ticket

Buy insurance

Get visa

Buy foreign exchange

Buy giftsFeatures of the graph

Directed

No cycles

Cyclic dependencies are unsatisfiable

Page 17: 8 Graphs 4

Directed Acyclic Graphs

Page 18: 8 Graphs 4

Directed Acyclic Graphs

G = (V,E), a directed graph

Page 19: 8 Graphs 4

Directed Acyclic Graphs

G = (V,E), a directed graph

No cycles

Page 20: 8 Graphs 4

Directed Acyclic Graphs

G = (V,E), a directed graph

No cycles

No directed path from any v in V back to itself

Page 21: 8 Graphs 4

Directed Acyclic Graphs

G = (V,E), a directed graph

No cycles

No directed path from any v in V back to itself

Such graphs are also called DAGs

Page 22: 8 Graphs 4

Topological ordering

Page 23: 8 Graphs 4

Topological ordering

Given a DAG G = (V,E), V = {1,2,…,n}

Enumerate the vertices as {i1,i2,…,in} so that

For any edge (j,k) in E,

j appears before k in the enumeration

Page 24: 8 Graphs 4

Topological ordering

Given a DAG G = (V,E), V = {1,2,…,n}

Enumerate the vertices as {i1,i2,…,in} so that

For any edge (j,k) in E,

j appears before k in the enumeration

Also known as topological sorting

Page 25: 8 Graphs 4

Topological ordering

Page 26: 8 Graphs 4

Topological orderingObservation

Page 27: 8 Graphs 4

Topological orderingObservation

A directed graph with cycles cannot be topologically ordered

Page 28: 8 Graphs 4

Topological orderingObservation

A directed graph with cycles cannot be topologically ordered

Path from j to k and from k to j means

j must come before k

k must come before j

Impossible!

Page 29: 8 Graphs 4

Topological ordering

Page 30: 8 Graphs 4

Topological orderingClaim

Every directed acyclic graph can be topologically ordered

Page 31: 8 Graphs 4

Topological orderingClaim

Every directed acyclic graph can be topologically ordered

Strategy

First list vertices with no incoming edges

Then list vertices whose incoming neighbours are already listed

Page 32: 8 Graphs 4

Topological ordering

Page 33: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

Page 34: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

outdegree(v): number of edges out of v

Page 35: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

outdegree(v): number of edges out of v

Every dag has at least one vertex with indegree 0

Page 36: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

outdegree(v): number of edges out of v

Every dag has at least one vertex with indegree 0

Start with any v such that indegree(v) > 0

Page 37: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

outdegree(v): number of edges out of v

Every dag has at least one vertex with indegree 0

Start with any v such that indegree(v) > 0

Walk backwards to a predecessor so long as indegree > 0

Page 38: 8 Graphs 4

Topological orderingindegree(v) : number of edges into v

outdegree(v): number of edges out of v

Every dag has at least one vertex with indegree 0

Start with any v such that indegree(v) > 0

Walk backwards to a predecessor so long as indegree > 0

If no vertex has indegree 0, within n steps we will complete a cycle!

Page 39: 8 Graphs 4

Topological ordering

Page 40: 8 Graphs 4

Topological orderingPick a vertex with indegree 0

No dependencies

Enumerate it and delete it from the graph

Page 41: 8 Graphs 4

Topological orderingPick a vertex with indegree 0

No dependencies

Enumerate it and delete it from the graph

What remains is again a DAG!

Page 42: 8 Graphs 4

Topological orderingPick a vertex with indegree 0

No dependencies

Enumerate it and delete it from the graph

What remains is again a DAG!

Repeat the step above

Stop when the resulting DAG is empty

Page 43: 8 Graphs 4

1 2

3

4 5

6 7 8

Page 44: 8 Graphs 4

1 2

3

4 5

6 7 8

Indegree0 0

2

11

2 1 4

Page 45: 8 Graphs 4

1 2

3

4 5

6 7 8

Indegree0 0

2

11

2 1 4

Page 46: 8 Graphs 4

2

3

4 5

6 7 8

Indegree0

2 1 4

1

00

1

Page 47: 8 Graphs 4

2

35

6 7 8

Indegree0

1

1

01

4

1 3

Page 48: 8 Graphs 4

35

6 7 8

Indegree

1

1

0

4

1

0

2

2

Page 49: 8 Graphs 4

3

6 7 8

Indegree

1

1 4

1

0

2

1

5

Page 50: 8 Graphs 4

6 7 8

Indegree

1

1 4 2

1

5

0

3

Page 51: 8 Graphs 4

7 8

Indegree

1 4 2

1

5 3 6

0

Page 52: 8 Graphs 4

8

Indegree

1 4 2 5 3 6 7

0

Page 53: 8 Graphs 4

Indegree

1 4 2 5 3 6 7 8

Page 54: 8 Graphs 4

Topological orderingfunction TopologicalOrder(G) for i = 1 to n indegree[i] = 0 for j = 1 to n indegree[i] = indegree[i] + A[j][i] ! for i = 1 to n choose j with indegree[j] = 0 enumerate j indegree[j] = -1 for k = 1 to n if A[j][k] == 1 indegree[k] = indegree[k]-1

Page 55: 8 Graphs 4

Topological ordering

Page 56: 8 Graphs 4

Topological ordering

Complexity is O(n2)

Page 57: 8 Graphs 4

Topological ordering

Complexity is O(n2)

Initializing indegree takes time O(n2)

Page 58: 8 Graphs 4

Topological ordering

Complexity is O(n2)

Initializing indegree takes time O(n2)

Loop n times to enumerate vertices

Inside loop, identifying next vertex is O(n)

Updating indegrees of neighbours is O(n)

Page 59: 8 Graphs 4

Topological ordering

Page 60: 8 Graphs 4

Topological orderingUsing adjacency list

Page 61: 8 Graphs 4

Topological orderingUsing adjacency list

Scan lists once to compute indegrees — O(m)

Page 62: 8 Graphs 4

Topological orderingUsing adjacency list

Scan lists once to compute indegrees — O(m)

Put all indegree 0 vertices in a queue

Page 63: 8 Graphs 4

Topological orderingUsing adjacency list

Scan lists once to compute indegrees — O(m)

Put all indegree 0 vertices in a queue

Enumerate head of queue and decrement indegree of neighbours — degree(j), overall O(m)

Page 64: 8 Graphs 4

Topological orderingUsing adjacency list

Scan lists once to compute indegrees — O(m)

Put all indegree 0 vertices in a queue

Enumerate head of queue and decrement indegree of neighbours — degree(j), overall O(m)

If indegree(k) becomes 0, add to queue

Page 65: 8 Graphs 4

Topological orderingUsing adjacency list

Scan lists once to compute indegrees — O(m)

Put all indegree 0 vertices in a queue

Enumerate head of queue and decrement indegree of neighbours — degree(j), overall O(m)

If indegree(k) becomes 0, add to queue

Overall O(n+m)

Page 66: 8 Graphs 4

Topological ordering revisitedfunction TopologicalOrder(G) //Edges are in adjacency list for i = 1 to n { indegree[i] = 0 } for i = 1 to n for (i,j) in E //proportional to outdegree(i) indegree[j] = indegree[j] + 1 for i = 1 to n if indegree[i] == 0 { add i to Queue } while Queue is not empty j = remove_head(Queue) for (j,k) in E //proportional to outdegree(j) indegree[k] = indegree[k] - 1 if indegree[k] == 0 { add k to Queue }

Page 67: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 68: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 69: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 70: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 71: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 72: 8 Graphs 4

1 2

3

4 5

6 7 8

Topological orderingImagine these are courses

Edges are pre-requisites

What is the minimum number of semesters to complete the programme?

Page 73: 8 Graphs 4

Longest path in a DAG

Page 74: 8 Graphs 4

Longest path in a DAG

Equivalent to finding longest path in the DAG

Page 75: 8 Graphs 4

Longest path in a DAG

Equivalent to finding longest path in the DAG

If indegree(j) = 0, longest_path_to(j) = 0

Page 76: 8 Graphs 4

Longest path in a DAG

Equivalent to finding longest path in the DAG

If indegree(j) = 0, longest_path_to(j) = 0

If indegree(k) > 0, longest_path_to(k) is

1 + max{ longest_path_to(j) } among all

incoming neighbours j of k

Page 77: 8 Graphs 4

Longest path in a DAG

Page 78: 8 Graphs 4

Longest path in a DAGTo compute longest_path_to(k)

Need longest_path_to(j) for all incoming neighbours of k

Page 79: 8 Graphs 4

Longest path in a DAGTo compute longest_path_to(k)

Need longest_path_to(j) for all incoming neighbours of k

If j is an incoming neighbour, (j,k) in E

j is enumerated before k in topological order

Page 80: 8 Graphs 4

Longest path in a DAGTo compute longest_path_to(k)

Need longest_path_to(j) for all incoming neighbours of k

If j is an incoming neighbour, (j,k) in E

j is enumerated before k in topological order

Hence, compute longest_path_to(i) in topological order

Page 81: 8 Graphs 4

Longest path in a DAG

Page 82: 8 Graphs 4

Longest path in a DAGLet i1,i2,…,in be a topological ordering of V

Page 83: 8 Graphs 4

Longest path in a DAGLet i1,i2,…,in be a topological ordering of V

All neighbours of ik appear before it in this list

Page 84: 8 Graphs 4

Longest path in a DAGLet i1,i2,…,in be a topological ordering of V

All neighbours of ik appear before it in this list

From left to right, compute longest_path_to(ik) as

1 + max{ longest_path_to(ij) } among all

incoming neighbours ij of ik

Page 85: 8 Graphs 4

Longest path in a DAGLet i1,i2,…,in be a topological ordering of V

All neighbours of ik appear before it in this list

From left to right, compute longest_path_to(ik) as

1 + max{ longest_path_to(ij) } among all

incoming neighbours ij of ik

Can combine this calculation with topological sort

Page 86: 8 Graphs 4

1 2

3

4 5

6 7 8

Indegree0 0

2

11

2 1 4

Longest Path 0 0

0

0

0 0 0

0

Page 87: 8 Graphs 4

2

3

4 5

6 7 8

Indegree0

2 1 4

1

00

1

Longest Path 0

0 0 0

1

11

0

Page 88: 8 Graphs 4

2

35

6 7 8

Indegree0

1

1

01

4

1 3

Longest Path 0

0

11

2 2

0 1

Page 89: 8 Graphs 4

35

6 7 8

Indegree

1

1

0

4

1

0

2

2

Longest Path

0

11

2 2

0 1 0

Page 90: 8 Graphs 4

3

6 7 8

Indegree

1

1 4

1

0

2

1

5

Longest Path

0

1

2 2

0 1 0 1

Page 91: 8 Graphs 4

6 7 8

Indegree

1

1 4 2

1

5

0

3

Longest Path

0 22

0 1 0 1 1

Page 92: 8 Graphs 4

7 8

Indegree

1 4 2

1

5 3 6

0

Longest Path

23

0 1 0 1 1 2

Page 93: 8 Graphs 4

8

Indegree

1 4 2 5 3 6 7

0

Longest Path

4

0 1 0 1 1 2 3

Page 94: 8 Graphs 4

Indegree

1 4 2 5 3 6 7 8

Longest Path

0 1 0 1 1 2 3 4

Page 95: 8 Graphs 4

Topological ordering with longest pathfunction TopologicalOrderWithLongestPath(G) for i = 1 to n indegree[i] = 0; LPT[i] = 0 for j = 1 to n indegree[i] = indegree[i] + A[j][i] ! for i = 1 to n choose j with indegree[j] = 0 enumerate j indegree[j] = -1 for k = 1 to n if A[j][k] == 1 indegree[k] = indegree[k]-1 LPT[k] = max(LPT[k], 1 + LPT[j])

Page 96: 8 Graphs 4

This implementation has complexity is O(n2)

As before, we can use adjacency lists to improve the complexity to O(m+n)

Topological ordering with longest path

Page 97: 8 Graphs 4

function TopologicalOrder(G) //Edges are in adjacency list for i = 1 to n { indegree[i] = 0; LPT[i] = 0} for i = 1 to n for (i,j) in E //proportional to outdegree(i) indegree[j] = indegree[j] + 1 for i = 1 to n if indegree[i] == 0 { add i to Queue } while Queue is not empty j = remove_head(Queue) for (j,k) in E //proportional to outdegree(j) indegree[k] = indegree[k] - 1 LPT[k] = max(LPT[k], 1 + LPT[j]) if indegree[k] == 0 { add k to Queue }

Topological ordering with longest path 2

Page 98: 8 Graphs 4

Summary

Dependencies are naturally modelled using DAGs

Topological ordering lists vertices without violating dependencies

Longest path in a DAG represents minimum number of steps to list all vertices in groups


Recommended