Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Structures and Algorithms I

Similar presentations


Presentation on theme: "Data Structures and Algorithms I"— Presentation transcript:

1 Data Structures and Algorithms I
CMP 338 Data Structures and Algorithms I Day 17, 11/1/11 (Undirected and Directed) Graphs

2 Homework 11 due: noon 11/4 Implement 2-3 trees directly (Algorithms 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

3 Homework 12 due: 11pm 11/8 Shortest path in a grid (Algorithms 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>

4 Graphs (Mathematics) (Directed) Graph <V, E>
V is a set of vertices E is a set of edges E  V x V DAG Directed Acyclic Graph Undirected Graph E is symmetric Edge-Weighted Graph weight: E → R

5 Graph Vocabulary A path is a sequence edges connecting vertices
simple path: no vertex appears twice A cycle is a path from a vertex to itself simple cycle: removing final edge leaves a simple path A connected component (undirected graph): A maximal set of connected vertices A strongly connected component (directed graph): A maximal set of vertices such that there is a directed path from any vertex to any other vertex

6 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

7 Depth-First Search Each node visited exactly once.
Visit each neighbor during visit to a node void visit (Node n) if (visited(n)) return; mark n visited do stuff for each neighbor m of n visit(m) maybe do more stuff Trace: (1(2(3)(4(5)(6))(7))(8(9)(A(B)(C))))(D(E)(F)) Example: ConnectedComponent.java

8 Breadth-First Search Each node visited exactly once.
Schedule visit to each neighbor during visit to a node void visit (Node n) if (visited(n)) return; mark n visited do stuff for each neighbor m of n put m on queue of nodes to visit maybe do more stuff Trace: (1)(2)(3)(6)(7)(8)(4)(5)(9)(A)(C)(B)(D)(E)(F) Example: ShortestPath.java


Download ppt "Data Structures and Algorithms I"

Similar presentations


Ads by Google