Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs CMP 338 Data Structures and Algorithms I Day 19, 11/3/11 Edge-Weighted Graphs
Homework 11 due: noon 11/4 Implement 2-3 trees directly (Algorithms 3.3.35 pg 452) public class Homework11 extends AbstractOrderedSymbolTable (or) implements OrderedSymbolTable Implement different classes for 2-Nodes and 3-Nodes Preserve 2-3 tree invariants: Keys are ordered in the 2-3 tree All paths from leaves to root are the same length Correctness test: FrequencyCounter on tale.txt
Homework 12 due: 11pm 11/8 Shortest path in a grid (Algorithms 4.4.33 p.g. 689) public class Homework12 public double length(double[][] grid) grid: NxM two-dimensional array of doubles > 0 Nodes: <r, c> where 0<=r<N and 0<=c<M Edges: <<r, c>, <r, c+1>> and <<r,c>, <r+1, c>> weight (<r, c>, <r', c'>): grid(r, c) + grid(r', c') return “length” of shortest path from <0, 0> to <N-1, M-1> Extra credit: also handle edges from <r, c> to <r-1, c> and <r, c-1>
Spanning Tree (Undirected Graph) A tree in an undirected graph: A set of connected edges not containing a cycle A spanning tree or an undirected graph: A tree that connects each vertex of the graph A spanning forest of an undirected graph: Set of spanning trees of the connected components A minimum spanning tree (MST) of a weighted graph The spanning tree with minimum total weight
Prim's MST Algorithm Use priority queue to keep track of the edges mark any node while exists an edge from marked to unmarked pick the shortest such edge add the edge to the MST mark the unmarked vertex Use priority queue to keep track of the edges Optimization: only 1 edge per unmarked node Need to be able to reduce a key in a priority queue Running-time: ||E|| + ||V|| lg ||V||
Kruskal's MST Algorithm while exists an unconsidered edge consider the shortest unconsidered edge if it would not create a cycle add the edge to MST Use priority queue to keep track of the edges Cycle detection: Disjoint Union / Find algorithm Edge will create a cycle iff both end-points in the same set Adding an edge to MST requires union of two sets Running-time: ||E|| lg ||E||
Shortest Path Algorithms Shortest paths in edge-weighted directed graphs Problem ill-formed if any negative cycle is reachable If graph is a DAG Relax nodes in topological order O(||E|| + ||V||) If all edges are non-negative (Dijkstra) Mark and relax nearest unmarked node O(||E||+||V|| lg ||V||) General edge-weighted directed graphs (Bellman-Ford) Repeat up to ||V|| times: Relax nodes changed in previous iteration O(||E|| ||V||)
Relaxation void relax (Node n) for Node m in edgeFrom(n) relax(n, m) void relax (Node n, Node m) If dist(s, n) + w(n, m) < dist(s, m) dist(s, m) = dist(s, n) + w(n, m) parent(m) = n dist is a Map<Node, Double> parent is a Map<Node, Node>
Dijkstra's Shortest Path Algorithm mark the source node while exists an edge from marked to unmarked pick closest unmarked node n to source pick shortest edge from marked to n add edge to Shortest-Path tree mark and relax n Use priority queue to order unmarked nodes Running-time: ||E|| + ||V|| lg ||V||