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