Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Reference:

Similar presentations


Presentation on theme: "Graphs Reference:"— Presentation transcript:

1 Graphs Reference: http://en.wikipedia.org/wiki/Graph_%28data_structure%29

2 Terminology A set of nodes (or vertices) connected by edges (or arcs). – Edges can be directed (one-way) or undirected (two-way) – A graph has a cycle if there is a path from a node back to itself.

3 A few applications Maps States of a Rubik’s (to find a solution) Speech models (for speech recognition) Regular Expressions (regex) ( see http://en.wikipedia.org/wiki/Regular_expression ) http://en.wikipedia.org/wiki/Regular_expression Compiler design …

4 Representation Two of the most common: – Adjacency List: a list of edges (for each) node of all the neighboring nodes. – Adjacency Matrix: a 2d array of edges “Apple” “Carrot” “Date”“Fox” “Eagle” “Bubble” 15 7 9 3 1 12 6 4

5 Adjacency List “Apple” “Carrot” “Date”“Fox” “Eagle” “Bubble” 15 7 9 3 1 12 6 4 “Apple”(15,”Carrot”) “Bubble”(4,”Date”) “Carrot”(9, “Fox”)(1, “Eagle”)(3, “Bubble”) “Date”(6, “Eagle”) “Eagle”(12, “Date”) “Fox”(7, “Apple”) Good for sparse graphs (like this one)

6 Adjacency Matrix “Apple” “Carrot” “Date”“Fox” “Eagle” “Bubble” 15 7 9 3 1 12 6 4 AppleBubbleCarrotDateEagleFox Apple--15--- Bubble---4-- Carrot-3--19 Date----6- Eagle---12-- Fox7----- Good for dense graphs

7 Common Algorithms Breadth-first search: spread out evenly from a seed node. Depth-first search: tunnel down from a seed node, backing up if you’ve visited all neighboring nodes. Both algorithms need to track visited nodes – Perhaps inside the node structure. – Perhaps as a separate tree structure (a search tree) Depth-first search is naturally done using recursion (or with a stack). Breadth-first search needs a vector (the frontier of the search)

8 Example Graph A C E D J F B G I H ABCDEFGHIJ A2 B6 C29113 D98 E18 F617 G75 H6 I564 J34 2 9 8 1 3 1 4 7 6 5 6

9 Breadth-first search A C E D J F B G I H ABCDEFGHIJ A2 B6 C29113 D98 E18 F617 G75 H6 I564 J34 2 9 8 1 3 1 4 7 6 5 6

10 Depth-First Search A C E D J F B G I H ABCDEFGHIJ A2 B6 C29113 D98 E18 F617 G75 H6 I564 J34 2 9 8 1 3 1 4 7 6 5 6

11 Pseudo-code On the board, come up with a prototype graph class (and Node class) with DFS and BFS algorithms (to print all nodes)

12 Floyd-Warshall Algorithm (1962) A slowish algorithm to find the shortest path which finds the shortest path from any node to any other node in the graph. (python) pseudocode 1.def floyd(adjMat, n): 2. Dist = matrix(n, n, infinity) 3. Next = matrix(n, n, None) 4. for v in range(0, n): 5. Dist[v][v] = 0.0 6. for i in range(0, n): 7. for j in range(0, n): 8. if isEdge(adjMat, i, j): 9. Dist[i][j] = adjMat.cost(i, j) 10. for k in range(0, n): 11. for i in range(0, n): 12. for j in range(0, n): 13. if Dist[i][k] + Dist[k][j] < Dist[i][j]: 14. Dist[i][j] = Dist[i][k] + Dist[k][j] 15. Next[i][j] = k

13 Floyd’s Algorithm ABCDEFGHIJ A2 B6 C29113 D98 E18 F617 G75 H6 I564 J34 A C E D J F B G I H 2 9 8 1 3 1 4 7 6 5 6


Download ppt "Graphs Reference:"

Similar presentations


Ads by Google