1 / 39

Data Structures and Algorithms for Information Processing

Data Structures and Algorithms for Information Processing. Lecture 7: Graphs. Today’s Topics. Graphs undirected graphs directed graphs Graph Traversals depth-first (recursive vs. stack) breadth-first (queue). Graph Definitions. Nodes and links between them

rafer
Download Presentation

Data Structures and Algorithms for Information Processing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures and Algorithms for Information Processing Lecture 7: Graphs Lecture 7: Graphs

  2. Today’s Topics • Graphs • undirected graphs • directed graphs • Graph Traversals • depth-first (recursive vs. stack) • breadth-first (queue) Lecture 7: Graphs

  3. 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

  4. Vertices are labelled V0 e0 Edges are labelled V2 V1 e1 e4 e5 e2 V4 V3 e3 Undirected Graphs • Vertices drawn with circles • Edges drawn with lines Visual Layout Doesn’t Matter! Lecture 7: Graphs

  5. 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

  6. 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

  7. Start Finish Graphing a Search Space 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

  8. 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

  9. 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

  10. 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

  11. Graph Terminology • Simple Graphs: have no loops and no multiple edges Lecture 7: Graphs

  12. 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

  13. 1 3 2 4 0 Adjacency Matrix 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 Lecture 7: Graphs

  14. Adjacency Matrix • Can be implemented with a two-dimensional array, e.g.:boolean[][] adjMatrix;adjMatrix = new boolean[5][5]; Lecture 7: Graphs

  15. 1 3 0 1 2 … 2 2 3 1 null null 4 0 4 null Edge Lists • Graphs can also be represented by creating a linked list for each vertex For each entry J in list number I, there is an edge from I to J. Lecture 7: Graphs

  16. Edge Sets • Use a previously-defined set ADT to hold the integers corresponding to target vertices from a given vertexIntSet[] edges = new IntSet[5]; • Quiz: Draw a Red Black Tree implementation of IntSet. Lecture 7: Graphs

  17. 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 Lecture 7: Graphs

  18. 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

  19. 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) Lecture 7: Graphs

  20. Programming Example • Simple, directed, labeled Graph • For generality, labels will be represented as references to Java’s Object class Lecture 7: Graphs

  21. Instance Variables public class Graph { private boolean[][] edges; private Object[] labels;} Lecture 7: Graphs

  22. Constructor public Graph (int n) { edges = new boolean[n][n]; labels = new Object[n];} Lecture 7: Graphs

  23. addEdge Method public void addEdge(int s, int t) { edges[s][t] = true;} Lecture 7: Graphs

  24. getLabel Method public Object getLabel(int v) { return labels[v];} Lecture 7: Graphs

  25. isEdge Method public boolean isEdge(int s, int t) { return edges[s][t];} Lecture 7: Graphs

  26. 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

  27. 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

  28. removeEdge Method public void removeEdge(int s, int t) { edges[s][t] = false;} Lecture 7: Graphs

  29. setLabel, size Methods public void setLabel(int v, Object n) { labels[v] = n;} public int size() {return labels.length;} Lecture 7: Graphs

  30. 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

  31. 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 visitedboolean[] marked;marked = new boolean[g.size()]; Lecture 7: Graphs

  32. 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

  33. Depth-First Traversal 0 2 1 4 6 3 5 Lecture 7: Graphs

  34. 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

  35. Breadth-First Traversal 0 2 1 4 6 3 5 Lecture 7: Graphs

  36. Depth-First Traversal public static void dfPrint (Graph g, int start) {boolean[] marked = new boolean[g.size()];dfRecurse(g, start, marked); } Lecture 7: Graphs

  37. 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

  38. Depth-First Example for (i=0;i<next.length;i++) { nextNeighbor = next[i]; if (!marked[nextNeighbor]) depthFirstRecurse(g, nextNeighbor, marked);} } Lecture 7: Graphs

  39. 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

More Related