Presentation is loading. Please wait.

Presentation is loading. Please wait.

90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.

Similar presentations


Presentation on theme: "90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures."— Presentation transcript:

1 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures and Algorithms for Information Processing Lecture 7: Graphs

2 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 2 Lecture 7: Graphs Today’s Topics Graphs –undirected graphs –directed graphs Graph Traversals –depth-first (recursive vs. stack) –breadth-first (queue)

3 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 3 Lecture 7: Graphs Graph Definitions Nodes and links between them May be linked in any pattern (unlike trees) Vertex: a node in the graph Edge: a connection between nodes

4 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 4 Lecture 7: Graphs Undirected Graphs Vertices drawn with circles Edges drawn with lines V0 V4 V3 V2 V1 Vertices are labelled Edges are labelled e3 e2 e0 e1 e4 e5 Visual Layout Doesn’t Matter!

5 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 5 Lecture 7: Graphs Undirected Graphs An undirected graph is a finite set of vertices along with a finite set of edges. The empty graph is an empty set of vertices and an empty set of edges. Each edge connects two vertices The order of the connection is unimportant

6 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 6 Lecture 7: Graphs Graphing a Search Space The “three coins” game (p. 692) (use green = heads, red = tails) Switch from green, red, green to red, green, red You may flip the middle coin any time End coins can flip only when the other two coins match each other

7 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 7 Lecture 7: Graphs Graphing a Search Space Start Finish Often, a problem can be represented as a graph, and finding a solution is obtained by performing an operation on the graph (e.g. finding a path)

8 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 8 Lecture 7: Graphs Directed Graphs A directed graph is a finite set of vertices and a finite set of edges; both sets may be empty to represent the empty graph. Each edge connects two vertices, called the source and target; the edge connects the source to the target (order is significant)

9 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 9 Lecture 7: Graphs Directed Graphs Useful for modelling directional phenomena, like geographical travel, or game-playing where moves are irreversible Examples: modelling states in a chess game, or Tic-Tac-Toe Arrows are used to represent the edges; arrows start at the source vertex and point to the target vertex

10 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 10 Lecture 7: Graphs Graph Terminology Loops: edges that connect a vertex to itself Paths: sequences of vertices p0, p1, … pm such that each adjacent pair of vertices are connected by an edge Multiple Edges: two nodes may be connected by >1 edge

11 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 11 Lecture 7: Graphs Graph Terminology Simple Graphs: have no loops and no multiple edges

12 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 12 Lecture 7: Graphs Graph Implementations Adjacency Matrix –A square grid of boolean values –If the graph contains N vertices, then the grid contains N rows and N columns –For two vertices numbered I and J, the element at row I and column J is true if there is an edge from I to J, otherwise false

13 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 13 Lecture 7: Graphs Adjacency Matrix 1 2 3 0 4 01234 0 falsefalse truefalsefalse 1falsefalse falsetruefalse 2falsetruefalsefalsetrue 3falsefalsefalsefalsefalse 4falsefalsefalsetruefalse

14 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 14 Lecture 7: Graphs Adjacency Matrix Can be implemented with a two- dimensional array, e.g.: boolean[][] adjMatrix; adjMatrix = new boolean[5][5];

15 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 15 Lecture 7: Graphs Edge Lists Graphs can also be represented by creating a linked list for each vertex 1 2 3 0 4 2 null 0 3 1 1 2 4 … For each entry J in list number I, there is an edge from I to J.

16 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 16 Lecture 7: Graphs Edge Sets Use a previously-defined set ADT to hold the integers corresponding to target vertices from a given vertex IntSet[] edges = new IntSet[5]; Quiz: Draw a Red Black Tree implementation of IntSet.

17 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 17 Lecture 7: Graphs Worst-Case Analysis Adding or Removing Edges –adjacency matrix: small constant –edge list: O(N) –edge set: O(logN) if B-Tree is used Checking if an Edge is Present –adjacency matrix: small constant –edge list: O(N) –edge set: O(logN) if B-Tree or Red Black tree is used

18 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 18 Lecture 7: Graphs Worst-Case Analysis Iterating through a Vertex’s Edges –adjacency matrix: O(N) –edge list: O(E) if there are E edges –edge set: O(E) if a B-Tree or Red Black tree implementation is used - In a dense graph, E will be at most N.

19 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 19 Lecture 7: Graphs Choice of Implementation Based on: –which operations are more frequent –whether a good set ADT is available –average number of edges per vertex (a matrix is wasteful for sparse graphs, takes (n 2 ) space).

20 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 20 Lecture 7: Graphs Programming Example Simple, directed, labeled Graph For generality, labels will be represented as references to Java’s Object class

21 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 21 Lecture 7: Graphs Instance Variables public class Graph { private boolean[][] edges; private Object[] labels; }

22 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 22 Lecture 7: Graphs Constructor public Graph (int n) { edges = new boolean[n][n]; labels = new Object[n]; }

23 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 23 Lecture 7: Graphs addEdge Method public void addEdge(int s, int t) { edges[s][t] = true; }

24 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 24 Lecture 7: Graphs getLabel Method public Object getLabel(int v) { return labels[v]; }

25 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 25 Lecture 7: Graphs isEdge Method public boolean isEdge(int s, int t) { return edges[s][t]; }

26 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 26 Lecture 7: Graphs neighbors Method public int[] neighbors(int v) { int i; int count; int [] answer; count = 0; for (i=0; i<labels.length; i++) { if (edges[v][i]) count++; }

27 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 27 Lecture 7: Graphs neighbors Method (cont.) answer = new int[count]; count = 0; for (I=0; I<labels.length; I++) { if (edges[v][I]) answer[count++] = I; } return answer; }

28 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 28 Lecture 7: Graphs removeEdge Method public void removeEdge(int s, int t) { edges[s][t] = false; }

29 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 29 Lecture 7: Graphs setLabel, size Methods public void setLabel(int v, Object n) { labels[v] = n; } public int size() { return labels.length; }

30 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 30 Lecture 7: Graphs Graph Traversals Start at a particular vertex Find all vertices that can be reached from the start by following every possible path (set of edges) Q: What could go wrong? (Hint: how do graphs differ from trees?) A: We have to be careful about detecting cycles

31 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 31 Lecture 7: Graphs Graph Traversals Cycle detection can be implemented with an array of boolean values representing “shading” on the vertices Each vertex is shaded once it is visited boolean[] marked; marked = new boolean[g.size()];

32 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 32 Lecture 7: Graphs Depth-First Traversal Use a stack (or recursion) to store set of vertices to be visited From a given vertex, visit neighbors I+1 … N only after traversing all possible edges from neighbor I

33 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 33 Lecture 7: Graphs Depth-First Traversal 0 1 2 63 4 5

34 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 34 Lecture 7: Graphs Breadth-First Traversal Use a queue to store set of vertices to be visited Visit all neighbors before visiting any of their neighbors

35 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 35 Lecture 7: Graphs Breadth-First Traversal 0 1 2 63 4 5

36 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 36 Lecture 7: Graphs Depth-First Traversal public static void dfPrint (Graph g, int start) { boolean[] marked = new boolean[g.size()]; dfRecurse(g, start, marked); }

37 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 37 Lecture 7: Graphs Depth-First Example public static void dfRecurse (Graph g, int v, boolean[] marked) { int [] next = g.neighbors(v); int i, nextNeighbor; marked[v] = true; System.out.println(g.getLabel(v)); (continued on next slide)

38 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 38 Lecture 7: Graphs Depth-First Example for (i=0;i<next.length;i++) { nextNeighbor = next[i]; if (!marked[nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked); } }

39 90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 39 Lecture 7: Graphs Breadth-First Implementation Uses a queue of vertex numbers The start vertex is processed, marked, placed in the queue Repeat until queue is empty: –remove a vertex v from the queue –for each unmarked neighbor u of v: process u, mark u, place u in the queue


Download ppt "90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures."

Similar presentations


Ads by Google