Topics Representation of Graphs Breadth-first Search Depth-first Search Topological Sort Elementary Graph Algorithms Source: Introduction to Algorithms, Second Edition by Cormen, Leiserson, Rivest & Stein 11/20/2005 DSAII_ElementaryGraphAlgs Representation of Graphs 2 Graphs Why graphs? Many real world situations have items that are connected to each other Example1: Traveling salesman problem Example2: Electronic circuits Example3: Control flow in computer programs Definition: Graph: A graph is a set of vertices and a set of edges that connect pairs distinct vertices Graph, G = (V, E) 11/20/2005 DSAII_ElementaryGraphAlgs 3 V = Vertices E = edges 11/20/2005 DSAII_ElementaryGraphAlgs 4 1 Vertices and Edges Types of Graphs Dense graphs Vertex: Vertex and Node are used interchangeably Use vertex when discussing graphs Use node when discussing representations of graph Sparse graphs 5 Takes the number of vertices and edges and returns a pointer to graph Query Operations: edges directed Returns the number of vertices true if directed, otherwise false search Modifiers: insert remove sort 11/20/2005 DSAII_ElementaryGraphAlgs For sparse matrices Array Adj of lists Adjacency Matrix returns the number of edges 6 Adjacency Lists Graph (v, e) vertices DSAII_ElementaryGraphAlgs Representations of Graphs Constructors: Each edge has an associated weight given by weight function, w w: E Æ R 11/20/2005 Graph ADT Edges have no direction Weighted graphs DSAII_ElementaryGraphAlgs Edges are one way Undirected graphs: A connection between two vertices is named as an edge, arc, and link Use edge for graphs and link for data structures 11/20/2005 On the average, the number of edges connected to a vertex is small |E| << |V|2 Directed Graphs (Digraphs) Edge: On the average the number of edges connected to a vertex is large |E| ≈ |V|2 7 For dense graphs Matrix of Boolean values Columns and Rows represent vertices If an edge is present then 1 is used, otherwise 0 11/20/2005 DSAII_ElementaryGraphAlgs 8 2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Adjacency Lists Consists of an array Adj of |V| lists One list for each vertex For vertex u, the list is Adj[u] In each adjacency list 11/20/2005 DSAII_ElementaryGraphAlgs 9 For each u ∈ V Contains all vertices immediately adjacent to u Adjacency list Adj[u] contains all vertices v edge(u, v) ∈ E Adj[u] does not follow any order If weighted graph, w(u, v) is stored with v 11/20/2005 DSAII_ElementaryGraphAlgs 10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Tutorial Discuss how you would write a search algorithm for graphs based on adjacency lists 11/20/2005 DSAII_ElementaryGraphAlgs 11 11/20/2005 DSAII_ElementaryGraphAlgs 12 3 Adjacency Matrices Some Properties of Adjacency Lists Graph G = (V, E) Assume vertices are numbered 1, 2,…,|V| Adjacency matrix representation is a For a directed graph Sum of lengths of all adjacency lists: |E| If G is an undirected graph Sum of lengths of all adjacency lists: 2|E| For all graphs, memory required is: Θ (V + E) 11/20/2005 DSAII_ElementaryGraphAlgs 13 Matrix A of size |V| x |V| A = (aij) 11/20/2005 Breadth First Search ⎧1 a =⎨ ij ⎩0 if (i, j ) ∈ E otherwise DSAII_ElementaryGraphAlgs 14 Problem Inputs: A graph G = (V, E) Source vertex s Output: Discover all the vertices in the graph Methods: 11/20/2005 DSAII_ElementaryGraphAlgs 15 Breadth-First Search (BFS) Depth-First Search (DFS) 11/20/2005 DSAII_ElementaryGraphAlgs 16 4 BSF: Expanding Discovery Boundary (1) BSF: Main Properties Discovering a vertex: Explores all the edges of graph G Computes distance from s to each reachable vertex Undiscovered vertex: A distance from a vertex to another vertex is the smallest number of edges It produces a breadth-first tree (BFT) Root s The path from s to vertex v in BFT is the shortest path from s to v in G DSAII_ElementaryGraphAlgs 17 A vertex that has not been visited by the algorithm BSF expands frontier between discovered and undiscovered vertices Works on both directed and undirected graphs 11/20/2005 Reaching a vertex for the first time Discovers all vertices at distance u from s, before discovering any vertex at distance u+1 11/20/2005 BSF: Expanding Discovery Boundary (2) What does breadth first mean? Using colors: Undiscovered vertices – white Completely discovered – black Boundary vertices – grey If a vertex u is black If v is adjacent to u Then v is either black or gray That is all vertices adjacent to a black vertex are discovered Vertices change colors in algorithm: White Æ Grey Æ Black How algorithm assigns colors: If a vertex u is gray Initialization – all nodes white First time discovered – grey Completely expanded - black 11/20/2005 DSAII_ElementaryGraphAlgs 18 Expansion Order in BSF Mechanism for Expanding boundary DSAII_ElementaryGraphAlgs If v is adjacent to u Then v can be white 19 11/20/2005 DSAII_ElementaryGraphAlgs 20 5 Using Queue for Representing Boundary Vertex Fields Queues hold all gray vertices We Dequeue the first vertex Predecessor or Parent (π) In BFT, if u is already discovered, and v is in adjacency list of u If you discover v, then add it to the tree u is parent of v Expand it Æ new vertices (not black vertices) Enqueue the new vertices color distance, d 11/20/2005 DSAII_ElementaryGraphAlgs 21 11/20/2005 DSAII_ElementaryGraphAlgs 22 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Functional Description of BFS Initialize all vertices to white color Initialize queue with source vertex color it gray Dequeue first element V Å expand it (adjacent vertices) If white vertex enqueue it repeat 11/20/2005 DSAII_ElementaryGraphAlgs 23 11/20/2005 DSAII_ElementaryGraphAlgs 24 6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 25 11/20/2005 DSAII_ElementaryGraphAlgs 26 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Depth First Search 11/20/2005 DSAII_ElementaryGraphAlgs 27 11/20/2005 DSAII_ElementaryGraphAlgs 28 7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 29 11/20/2005 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs DSAII_ElementaryGraphAlgs 30 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 31 11/20/2005 DSAII_ElementaryGraphAlgs 32 8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topological Sort 11/20/2005 DSAII_ElementaryGraphAlgs 33 11/20/2005 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs DSAII_ElementaryGraphAlgs 34 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 35 11/20/2005 DSAII_ElementaryGraphAlgs 36 9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 37 11/20/2005 DSAII_ElementaryGraphAlgs 38 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 11/20/2005 DSAII_ElementaryGraphAlgs 39 10