Data Structures and Algorithms for Information Processing

Slides:



Advertisements
Similar presentations
CS203 Lecture 15.
Advertisements

Graphs CSC 220 Data Structure. Introduction One of the Most versatile data structures like trees. Terminology –Nodes in trees are vertices in graphs.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
Graphs Chapter 12. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Graph.
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Using nextAdjacent() To list all vertices connected to a vertex u in graph G we can use the following loop: for (int v=G.nextAdjacent(u,-1); v>=0; v=G.nextAdjacent(u,v))
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Graph COMP171 Fall Graph / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D E A C F B Vertex Edge.
Hashing with Separate Chaining Implementation – data members public class SCHashTable implements HashTableInterface { private List [] table; private int.
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
Fall 2007CS 2251 Graphs Chapter 12. Fall 2007CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs To.
Graphs & Graph Algorithms Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
CS 206 Introduction to Computer Science II 03 / 30 / 2009 Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Representing and Using Graphs
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Common final examinations When: Wednesday, 12/11, 3:30-5:30 PM Where: Ritter Hall - Walk Auditorium 131 CIS 1166 final When: Wednesday, 12/11, 5:45-7:45.
Lecture 14 Graph Representations. Graph Representation – How do we represent a graph internally? – Two ways adjacency matrix list – Adjacency Matrix For.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
Graphs. Made up of vertices and arcs Digraph (directed graph) –All arcs have arrows that give direction –You can only traverse the graph in the direction.
Graphs. Definitions A graph is two sets. A graph is two sets. –A set of nodes or vertices V –A set of edges E Edges connect nodes. Edges connect nodes.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Graphs – Part II CS 367 – Introduction to Data Structures.
GRAPHS. Chapter Objectives  To become familiar with graph terminology and the different types of graphs  To study a Graph ADT and different implementations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 13: Graphs Data Abstraction & Problem Solving with C++
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
COSC 2007 Data Structures II
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 7: Graphs Data Structures.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs Chapter 15 introduces graphs which are probably the most general and commonly-used data structure. This lecture introduces heaps, which are used.
CS212: Data Structures and Algorithms
Data Structures Graphs - Terminology
Graphs Representation, BFS, DFS
Data Structures 13th Week
Data Structures and Algorithms
Csc 2720 Instructor: Zhuojun Duan
Graph Search Lecture 17 CS 2110 Fall 2017.
Common final examinations
CMSC 341 Lecture 21 Graphs (Introduction)
Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Graph transversals.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Search Related Algorithms
Graphs.
Chapter 11 Graphs.
Graphs Part 2 Adjacency Matrix
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
CSE 373 Data Structures Lecture 16
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graphs Chapter 7 Visit for more Learning Resources.
Graph Implementation.
Graph Traversal Lecture 18 CS 2110 — Spring 2019.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Analysis and design of algorithm
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Presentation transcript:

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