Data Structures and Algorithms for Information Processing Lecture 7: Graphs Lecture 7: Graphs
Today’s Topics Graphs Graph Traversals undirected graphs depth-first (recursive vs. stack) breadth-first (queue) 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 Lecture 7: Graphs
Undirected Graphs Vertices drawn with circles Edges drawn with lines Vertices are labelled Edges are labelled e3 e2 e0 e1 e4 e5 Visual Layout Doesn’t Matter! 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 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 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) 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) Lecture 7: Graphs
Directed Graphs Arrows are used to represent the edges; arrows start at the source vertex and point to the target vertex 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 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 Lecture 7: Graphs
Graph Terminology Simple Graphs: have no loops and no multiple edges 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 Lecture 7: Graphs
Adjacency Matrix 0 1 2 3 4 0 false false true false false 0 1 2 3 4 0 false false true false false 1 false false false true false 2 false true false false true 3 false false false false false 4 false false false true false 1 2 3 4 Lecture 7: Graphs
Adjacency Matrix Can be implemented with a two-dimensional array, e.g.: boolean[][] adjMatrix; adjMatrix = new boolean[5][5]; Lecture 7: Graphs
Edge Lists Graphs can also be represented by creating a linked list for each vertex 2 null 3 null 1 1 2 4 null … 1 2 3 4 For each entry J in list number I, there is an edge from I to J. 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. 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 edge set: O(logN) if B-Tree or Red Black tree is used 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. 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 (n2) space). Lecture 7: Graphs
Programming Example Simple, directed, labeled Graph For generality, labels will be represented as references to Java’s Object class Lecture 7: Graphs
Instance Variables public class Graph { private boolean[][] edges; private Object[] labels; } Lecture 7: Graphs
Constructor public Graph (int n) { edges = new boolean[n][n]; labels = new Object[n]; } Lecture 7: Graphs
addEdge Method public void addEdge(int s, int t) { edges[s][t] = true; } Lecture 7: Graphs
getLabel Method public Object getLabel(int v) { return labels[v]; } Lecture 7: Graphs
isEdge Method public boolean isEdge(int s, int t) { return edges[s][t]; } 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++; } 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; } Lecture 7: Graphs
removeEdge Method public void removeEdge(int s, int t) { edges[s][t] = false; } Lecture 7: Graphs
setLabel, size Methods public void setLabel(int v, Object n) { labels[v] = n; } public int size() { return labels.length; } 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 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()]; 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 Lecture 7: Graphs
Depth-First Traversal 2 1 4 6 3 5 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 Lecture 7: Graphs
Breadth-First Traversal 2 1 4 6 3 5 Lecture 7: Graphs
Depth-First Traversal public static void dfPrint (Graph g, int start) { boolean[] marked = new boolean[g.size()]; dfRecurse(g, start, marked); } 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) Lecture 7: Graphs
Depth-First Example for (i=0;i<next.length;i++) { nextNeighbor = next[i]; if (!marked[nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked); } } 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 Lecture 7: Graphs
DFS Without Recursion DFS(G,v) Make empty stack S for each vertex u, set visited[u] := false; push v onto s while (S is not empty) { u := pop S; if (not visited[u]) { visited[u] := true; for each unvisited neighbor w of u push S, w; } Lecture 7: Graphs
Runtime complexity DFS or BFS: - If adjacency matrix is used: Theta(n + n^2) = Theta(n^2) We need to visit n vertices. Theta(n). For each vertex visited, we need to examine n edges. Theta(n^2) - If adjacency list is used: Theta(n + e) where e is the number of edges in the graph. e <= n^2. Lecture 7: Graphs