Lecture 5: Graph Theory and Longest Paths © J. Christopher Beck 2008 1 Outline What is Graph Theory? Graph Theory Basics Finding the Longest Path Why? (modeling) How © J. Christopher Beck 2008 2 Readings Gibbons, Algorithmic Graph Theory, Cambridge University Press, 1985 Sedgewick, Algorithms in C++, 3rd Edition, 2002 Wikipedia (“Graph Theory”), accessed August 12, 2008 © J. Christopher Beck 2008 3 The Bridges of Konigsberg Can you walk a route that crosses each bridge exactly once? Euler, 1736 Islands © J. Christopher Beck 2008 Bridges 4 The Bridges of Konigsberg Edge or arc Node or vertex © J. Christopher Beck 2008 5 The Bridges of Konigsberg So, now the problem is: can you traverse every edge exactly once? Any ideas? © J. Christopher Beck 2008 6 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 © J. Christopher Beck 2008 7 Graph Theory Graph theory is the secret to the universe 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, … © J. Christopher Beck 2008 8 CPM Can we pose this as a graph theory problem? Why would we want to? © J. Christopher Beck 2008 9 CPM Model Job Node Precedence Directed Edge Processing time Edge Weight (This is the job-on-arc format: P p. 52) © J. Christopher Beck 2008 10 Our Small Example 1 Job p(j) Predecessors 1 2 2 3 3 1 4 4 1,2 5 2 2 6 1 4 2 4 3 5 1 2 3 © J. Christopher Beck 2008 6 2 3 3 4 4 6 5 What are we missing? 11 Dummy Nodes 1 2 3 4 6 5 0 0 0 0 1 2 3 2 3 3 4 4 6 2 5 1 7 1 © J. Christopher Beck 2008 Two dummy nodes (0 and n+1) 12 Makespan? How do we find the makespan? Critical Path? 0 0 0 0 1 2 3 2 3 3 4 4 6 2 5 1 7 1 © J. Christopher Beck 2008 13 Initialize Nodes & Queue class 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) ; © J. Christopher Beck 2008 14 Process Each Node while(!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); } © J. Christopher Beck 2008 After executing this, where is the makespan? 15 Critical Path How would you modify this algorithm to also allow you to find a critical path? © J. Christopher Beck 2008 16 Add a Field class Node { public int label; public int numPredecessors; public int head; public Node criticalPredecessor; }; © J. Christopher Beck 2008 17 Add a Line Then follow the criticalPredecessor links from nodeLast back to node0. while(!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); Does this form of algorithm (i.e., use of © J. Christopher Beck 2008 criticalPredecessor) remind you of anything? } 18 Example 4.2.3 Jobs 1 2 3 4 pj 5 6 7 8 9 10 11 12 13 14 5 6 9 12 7 12 10 6 10 9 7 8 7 5 Model and solve using a graph © J. Christopher Beck 2008 19 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. © J. Christopher Beck 2008 20
© Copyright 2025 Paperzz