© j. christopher beck 20081 lecture 5: graph theory and longest paths

20
© J. Christopher Beck 2008 1 Lecture 5: Graph Theory and Longest Paths

Upload: chastity-craig

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 1

Lecture 5: Graph Theory andLongest Paths

Page 2: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 2

Outline What is Graph Theory? Graph Theory Basics Finding the Longest Path

Why? (modeling) How

Page 3: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 3

Readings

Gibbons,Algorithmic Graph Theory, Cambridge University Press, 1985

Sedgewick, Algorithms in C++, 3rd Edition, 2002

Wikipedia (“Graph Theory”), accessed August 12, 2008

Page 4: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 4

The Bridges of Konigsberg

Can you walk a route that crosses each bridge exactly once?

Euler, 1736Islands Bridges

Page 5: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 5

The Bridges of Konigsberg

Node or vertex

Edge or arc

Page 6: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 6

The Bridges of Konigsberg

So, now the problem is: can you traverse every edge exactly once? Any ideas?

Page 7: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 7

Graph Theory

Nodes and edges form a graph, G(V,E), with V being the set of nodes, E being the set of edges

Edges may be directed or un-directed

Edges may have a weight Just a number associated with each

edge

Page 8: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 8

Graph Theory

Graph theory is the secret to theuniverse

A tremendous number of problems can be modeled and solved using graph theory OR, epidemiology, geometry, IT (the web

is a graph), social networks, biological pathways, chemical reactions, …

Page 9: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 9

CPM

Can we pose this as agraph theory problem? Why would we want to?

Page 10: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 10

CPM Model

Job Node Precedence Directed Edge Processing time Edge Weight

(This is the job-on-arc format: P p. 52)

Page 11: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 11

Our Small Example

Job p(j) Predecessors1 2 -2 3 -3 1 -4 4 1,25 2 26 1 4

1

2

3

64

5

1

2

3

64

5

2

3

3

4

What are we missing?

Page 12: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 12

Dummy Nodes1

2

3

64

5

0

0

0

0

1

2 64

5

2

3

3

4

7

1

2

1

Two dummy nodes(0 and n+1)

3

Page 13: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 13

Makespan?

0

0

0

0

1

2 64

5

2

3

3

4

7

1

2

1

3

How do we find the makespan?

Critical Path?

Page 14: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 14

Initialize Nodes & Queueclass Node {

public int label;

public int numPredecessors;

public int head;

};

// initialize Nodes.label and numPredecessors

// from input data. head = 0

Node node0 = initial dummy node

Node nodeLast = final dummy node

queue.push(node0) ;

Page 15: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 15

Process Each Nodewhile(!queue.empty()) {

Node n = queue.pop();

for each e = out-going edge of n

Node next = e.getOtherNode(n);

if (n.head + e.weight > next.head)

next.head = n.head + e.weight;

--next.numPredecessors;

if (next.numPredecessors == 0)

queue.push(next);

}After executing this, where is the makespan?

Page 16: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 16

Critical Path

How would you modify this algorithm to also allow you to find a critical path?

Page 17: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 17

Add a Fieldclass Node {

public int label;

public int numPredecessors;

public int head;

public Node criticalPredecessor;

};

Page 18: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 18

Add a Linewhile(!queue.empty()) {

Node n = queue.pop();

for each e = out-going edge of n

Node next = e.getOtherNode(n);

if (n.head + e.weight > next.head)

next.head = n.head + e.weight;

next.criticalPredecessor = n;

--next.numPredecessors;

if (next.numPredecessors == 0)

queue.push(next);

}

Then follow thecriticalPredecessor links

from nodeLast backto node0.

Does this form of algorithm (i.e., use ofcriticalPredecessor) remind you of anything?

Page 19: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 19

Example 4.2.3

Jobs 1 2 3 4 5 6 7 8 9 10

11

12

13

14

pj 5 6 9 12

7 12

10

6 10

9 7 8 7 5

Model and solve using a graph

Page 20: © J. Christopher Beck 20081 Lecture 5: Graph Theory and Longest Paths

© J. Christopher Beck 2008 20

The Bridges of Konigsberg

So, now the problem is: can you traverse every edge exactly once? Any ideas?

Hint: The degree of a node is the number of its edges.Think about the degrees of the nodes in any path.