Graphs. Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible.

Slides:



Advertisements
Similar presentations
Lecture 15. Graph Algorithms
Advertisements

Midwestern State University Department of Computer Science Dr. Ranette Halverson CMPS 2433 – CHAPTER 4 GRAPHS 1.
Review Binary Search Trees Operations on Binary Search Tree
Graph Theory.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
TECH Computer Science Graphs and Graph Traversals  // From Tree to Graph  // Many programs can be cast as problems on graph Definitions and Representations.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
Data Structures Using C++
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.
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.
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.
Introduction to Graphs
Graph & BFS Lecture 22 COMP171 Fall Graph & BFS / Slide 2 Graphs * Extremely useful tool in modeling problems * Consist of: n Vertices n Edges D.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
Graphs. Graphs Many interesting situations can be modeled by a graph. Many interesting situations can be modeled by a graph. Ex. Mass transportation system,
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
CS 206 Introduction to Computer Science II 11 / 05 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
CS Data Structures Chapter 6 Graphs.
Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices. Edges are.
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
Review of Graphs A graph is composed of edges E and vertices V that link the nodes together. A graph G is often denoted G=(V,E) where V is the set of vertices.
C o n f i d e n t i a l HOME NEXT Subject Name: Data Structure Using C Unit Title: Graphs.
GRAPHS Education is what remains after one has forgotten what one has learned in school. Albert Einstein Albert Einstein Smitha N Pai.
Graph. Data Structures Linear data structures: –Array, linked list, stack, queue Non linear data structures: –Tree, binary tree, graph and digraph.
Data Structures Using C++ 2E
Theory of Computing Lecture 10 MAS 714 Hartmut Klauck.
Copyright Networking Laboratory Chapter 6. GRAPHS Horowitz, Sahni, and Anderson-Freed Fundamentals of Data Structures in C, 2nd Edition Computer.
Been-Chian Chien, Wei-Pang Yang, and Wen-Yang Lin 6-1 Chapter 6 Graphs Introduction to Data Structure CHAPTER 6 GRAPHS 6.1 The Graph Abstract Data Type.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Chapter 2 Graph Algorithms.
GRAPHS CSE, POSTECH. Chapter 16 covers the following topics Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component,
© 2015 JW Ryder CSCI 203 Data Structures1. © 2015 JW Ryder CSCI 203 Data Structures2.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
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.
Data Structures & Algorithms Graphs
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
Graphs A graphs is an abstract representation of a set of objects, called vertices or nodes, where some pairs of the objects are connected by links, called.
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
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
Graphs 황승원 Fall 2010 CSE, POSTECH. 2 2 Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects.
GRAPHS. Graph Graph terminology: vertex, edge, adjacent, incident, degree, cycle, path, connected component, spanning tree Types of graphs: undirected,
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Chapter 6 Graphs. 2 Outline Definitions, Terminologies and Applications Graph Representation Elementary graph operations Famous Graph Problems.
Graphs Upon completion you will be able to:
Chapter 9: Graphs.
Graphs and Paths : Chapter 15 Saurav Karmakar
Chapter 20: Graphs. Objectives In this chapter, you will: – Learn about graphs – Become familiar with the basic terminology of graph theory – Discover.
Chapter 05 Introduction to Graph And Search Algorithms.
Data Structures and Algorithm Analysis Graph Algorithms Lecturer: Jing Liu Homepage:
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 GRAPHS – Definitions A graph G = (V, E) consists of –a set of vertices, V, and –a set of edges, E, where each edge is a pair (v,w) s.t. v,w  V Vertices.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
BCA-II Data Structure Using C Submitted By: Veenu Saini
Graphs Representation, BFS, DFS
Data Structures 13th Week
Graphs All tree structures are hierarchical. This means that each node can only have one parent node. Trees can be used to store data which has a definite.
Graphs Chapter 11 Objectives Upon completion you will be able to:
Graphs.
Chapter 11 Graphs.
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:

Graphs

Motivating Problem Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): C A B D ab c d e f g Starting in one land area, is it possible to walk across all bridges once and return to the initial land area? The answer is no, but how do we prove that?

Konigsberg = Graph Problem The Konigsberg is an instance of a graph problem The Konigsberg is an instance of a graph problem Definition of a graph: Definition of a graph: Graph G: Consists of two sets, V and E Graph G: Consists of two sets, V and E V: A finite, non-empty set of vertices V: A finite, non-empty set of vertices E: A set of pairs of vertices, where the pairs are called edges. E: A set of pairs of vertices, where the pairs are called edges.

Example Graphs V: 0, 1, 2, 3 E: (0,1), (0,2), (0,3) (1,2), (1,3), (2,3) V: 0, 1, 2, 3 E: Empty V: 0, 1, 2, 3 E: (0,1), (0,2), (1,3) Trees are a subset of graphs

Graph Definitions Undirected graph: Pair of vertices representing any edge is unordered Undirected graph: Pair of vertices representing any edge is unordered (u,v) is the same edge as (v,u) (u,v) is the same edge as (v,u) Directed graph: Each edge is represented by a directed pair (u,v) Directed graph: Each edge is represented by a directed pair (u,v) Drawn with an arrow from u to v indicating the direction of the edge Drawn with an arrow from u to v indicating the direction of the edge (u,v) is not the same edge as (v,u) (u,v) is not the same edge as (v,u)

Directed vs. Undirected Graph A: Undirected (1,0) is a valid edge Same as (0,1) edge Graph B: Directed Not equivalent to A (1,0) not valid edge Graph C: Directed Equivalent to A (1,0) is a valid edge A (0,1) edge also exists

Graph Restrictions For now lets assume vertices and edges are sets For now lets assume vertices and edges are sets No self edges (vertice back to itself) No self edges (vertice back to itself) No repeated edges (multigraph) No repeated edges (multigraph) Repeated Self

Motivating Problem: Graph Restrictions Konigsberg bridge problem (1736): Konigsberg bridge problem (1736): This is the appropriate graph representation. We’re not going to solve it for now because of our assumption of no repeated edges.

Graph Definitions Maximum possible number of distinct unordered pairs (u,v) (undirected graph) in a graph with n vertices is n*(n-1) / 2. Maximum possible number of distinct unordered pairs (u,v) (undirected graph) in a graph with n vertices is n*(n-1) / 2. A graph with this many edges is called a complete graph. A graph with this many edges is called a complete graph Complete: 6 edges = (4 * 3) / Not Complete: (4*3)/2 != 4 edges

Graph Definitions Directed Graph: Directed Graph: Maximum of (n * (n-1)) edges. [Twice that for undirected because 2 directed are equivalent to one undirected] Maximum of (n * (n-1)) edges. [Twice that for undirected because 2 directed are equivalent to one undirected] Proof of (n * (n – 1)) bounds: n nodes, can point to every other node except for themselves n-1 edges connecting to each of the n nodes

Graph Definitions If (u, v) is an edge in E(G), If (u, v) is an edge in E(G), Vertices u and v are called adjacent Vertices u and v are called adjacent The edge (u,v) is called incident on vertices u and v. The edge (u,v) is called incident on vertices u and v. Examples: Examples: Vertex 0 is adjacent to 1 and 2 Vertex 1 is adjacent to 0, 2, and 3 Vertex 2 is adjacent to 0 and 1 Vertex 3 is adjacent to 1 Edges incident on vertex 2: (0,2), (1,2) Edges incident on vertex 3: (1,3)

Graph Definitions A subgraph of graph G called G’ is a graph such that V(G’) is a subset V(G) and E(G’) is a subset of E(G) A subgraph of graph G called G’ is a graph such that V(G’) is a subset V(G) and E(G’) is a subset of E(G) Subgraphs

Graph Definitions A path from vertex u to vertex v in a graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) are edges in G. A path from vertex u to vertex v in a graph G is a sequence of vertices u, i1, i2, …, ik, v, such that (u,i1), (i1,i2)…(ik,v) are edges in G. If in a directed graph, the edges have to be in the right direction. If in a directed graph, the edges have to be in the right direction. The length of a path is the number of edges on the path. The length of a path is the number of edges on the path. A simple path is a path in which all vertices except possibly the first and last are distinct. A simple path is a path in which all vertices except possibly the first and last are distinct.

Graph Definitions Paths from 1 to 3: (1,3) 1,3 [Simple] Length = 1 (1,2), (2,3)1,2,3 [Simple] Length = 2 (1,0),(0,2),(2,1),(1,3)1,0,2,1,3 Length = 4 (1,2),(2,0),(0,1),(1,3)1,2,0,1,3 Length = 4 (1,0),(0,2),(2,3)1,0,2,3 [Simple] Length = 3 Many more that repeat internally, Not simple, Length > 4

Graph Definitions A cycle is a simple path where the first and last vertices are the same. A cycle is a simple path where the first and last vertices are the same Cycles to 1: 1,0,2,3,1 1,0,2,1 1,3,2,1 1,2,1 1,0,1 1,3,1

Graph Definitions Two vertices u and v are connected if there is a path in G from u to v. Two vertices u and v are connected if there is a path in G from u to v. An undirected graph is said to be connected (at the graph level) if and only if for every pair of distinct vertices u and v in V(G) there is a path from u to v in G. An undirected graph is said to be connected (at the graph level) if and only if for every pair of distinct vertices u and v in V(G) there is a path from u to v in G. A connected component of a graph is a maximal connected subgraph A connected component of a graph is a maximal connected subgraph

Graph Definitions Graph G4: V(G4): 0,1,2,3,4,5,6,7 E(G4): (0,1), (0,2), (1,3), (2,3), (4,5), (5,6), (6,7) There are two connected components of G4: H1 (0-3) and H2 (4-7) Verify that H1 and H2 components are connected: Path between all pairs of vertices Directed graphs – different because paths are directed, harder to get connected components

Graph Definitions A tree is a connected, acyclic graph A tree is a connected, acyclic graph For any node there is path to any other node (usually back through a “parent node”) For any node there is path to any other node (usually back through a “parent node”) Acyclic property forces a unique path between nodes Acyclic property forces a unique path between nodes

Graph Definitions Need a corollary to “connected” for directed graphs Need a corollary to “connected” for directed graphs A directed graph G is strongly connected if for every pair of distinct vertices in V(G), u and v, there is a directed path from u to v and from v to u. A directed graph G is strongly connected if for every pair of distinct vertices in V(G), u and v, there is a directed path from u to v and from v to u. A strongly connected component is a maximal subgraph of a directed graph that is strongly connected A strongly connected component is a maximal subgraph of a directed graph that is strongly connected

Graph Definitions 0 1 Graph G5: V(G5): 0,1,2 E(G5): (0,1), (1,0), (1,2) G5 is not strongly connected (No path from 2 to 1) There are two strongly connected components of G4: H1 (0-1) and H2 (2) Verify that H1 and H2 components are strongly connected: Directed path between all pairs of vertices 2

Graph Definitions The degree of a vertex v is the number of edges incident to that vertex. The degree of a vertex v is the number of edges incident to that vertex. For a directed graph, For a directed graph, The in-degree of a vertex v is the number of edges for which the arrow points at v. The in-degree of a vertex v is the number of edges for which the arrow points at v. The out-degree is defined as the number of edges for which the arrow points away from v. The out-degree is defined as the number of edges for which the arrow points away from v.

Graph Definitions Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree Out-Degree : 1 In-Degree: 0 1 Out-Degree : 1 In-Degree: 0 2 Out-Degree : 0 In-Degree: 2

Graph Definitions For undirected graph whose vertices v_i have degree d_i, the number of edges, |E|, is (the sum from 0 to n- 1 of degree_i ) / 2 For undirected graph whose vertices v_i have degree d_i, the number of edges, |E|, is (the sum from 0 to n- 1 of degree_i ) / 2 Essentially just counting the edges. Essentially just counting the edges. Divide by 2 because double counting (if node1, node2 share an edge, that edge is in both of their degrees) Divide by 2 because double counting (if node1, node2 share an edge, that edge is in both of their degrees) Useful for computing max number of edges if you only know number of vertices and their degree (ie they are all binary => |E| = 2 * |V| / 2 = |V|) Useful for computing max number of edges if you only know number of vertices and their degree (ie they are all binary => |E| = 2 * |V| / 2 = |V|) Given our graph assumptions (no repeated edges, no self edges) degree has to be <= |V|-1 Given our graph assumptions (no repeated edges, no self edges) degree has to be <= |V|-1

Graph Definitions Degree of Vertices: 0, 3 => Degree 2 1, 2 => Degree 3 |E| = Sum of Degrees / 2 = ( )/2 = 10/2 = 5 {Correct!} 0 12 |E| max = Sum of Degrees / 2 = ( ) / 2 = 6/2 = 3 {Correct!} |E| Max = ?, All Binary

Graph Representations What core functionality do we need in representation? What core functionality do we need in representation? Set of vertices Set of vertices Set of edges Set of edges Two major representations: Two major representations: Adjacency matrix [Array based] Adjacency matrix [Array based] Adjacency list [Linked List based] Adjacency list [Linked List based]

Adjacency Matrix G = (V,E) graph with |V| = n, n >= 1 G = (V,E) graph with |V| = n, n >= 1 Adjacency matrix: Adjacency matrix: 2 dimensional n x n array called A with property that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise. 2 dimensional n x n array called A with property that A[i][j] = 1 if the edge (i,j) is in E, 0 otherwise V0V1V2V3 V00110 V11001 V21001 V30110

Adjacency Matrix Directed Graphs: Rows are the out indicators (data in a row indicates that there is an outgoing link) Directed Graphs: Rows are the out indicators (data in a row indicates that there is an outgoing link) V0V1V2V3 V00010 V11001 V20000 V30010

Adjacency Matrix Note how the adjacency matrix for an undirected graph is symmetric. Can save approximately half the space by storing only upper triangle or lower triangle Note how the adjacency matrix for an undirected graph is symmetric. Can save approximately half the space by storing only upper triangle or lower triangle V0V1V2V3 V00110 V11001 V21001 V30110

Adjacency Matrix V0V1V2V3 V00110 V11001 V21001 V30110 Given a complete adjacency matrix, can easily: Determine if there is an edge between any two vertices (look in appropriate column) [Undirected Graph] Compute degree of a node (sum over row)

Adjacency Matrix Given a complete adjacency matrix, can easily: [Directed Graph] Compute out degree of a node (sum over row) Compute in degree of a node (sum over column) V0V1V2V3 V00010 V11001 V20000 V30010

Adjacency Matrix What if we want to compute a non-trivial answer? What if we want to compute a non-trivial answer? How many total edges are there in the graph? How many total edges are there in the graph? Is the graph connected? Is the graph connected? Total edges: Requires O(n^2) operations Total edges: Requires O(n^2) operations (n^2 entries – n [diagonals always 0]) (n^2 entries – n [diagonals always 0]) V0V1V2V3 V00110 V11001 V21001 V30110

Adjacency Matrix What if we have a sparse graph? What if we have a sparse graph? Sparse = Very few connections out of all possible Sparse = Very few connections out of all possible V0V0V0V0 V1V1V1V1 V2V2V2V2 V3V3V3V3 V4V4V4V4 V5V5V5V5 V6V6V6V6 V7V7V7V7 V V V V V V V V

Adjacency Matrix Would really like to do O(|E|) operations when counting edges Would really like to do O(|E|) operations when counting edges O(n^2) is a given when using adjacency matrix O(n^2) is a given when using adjacency matrix For dense graphs, |E| is close to n^2 For dense graphs, |E| is close to n^2 Not for sparse graphs (|E| << n^2) Not for sparse graphs (|E| << n^2) Solution: Use linked lists and store only those edges that are really represented in the graph (no 0’s for things that aren’t present). Solution: Use linked lists and store only those edges that are really represented in the graph (no 0’s for things that aren’t present). Slightly more complicated to implement but saves a lot of time Slightly more complicated to implement but saves a lot of time

Adjacency List N rows of adjacency matrix (vertices) are represented as n linked lists. N rows of adjacency matrix (vertices) are represented as n linked lists. Nodes in list i are those nodes adjacent to the corresponding vertex v_i. Nodes in list i are those nodes adjacent to the corresponding vertex v_i Order in linked list doesn’t matter Array of Head Node Pointers

Adjacency List Undirected Graph: n vertices, e edges – Requires n head nodes, 2 * e list nodes Undirected Graph: n vertices, e edges – Requires n head nodes, 2 * e list nodes For any vertex, computing degree (number of incident edges) is counting size of corresponding list. For any vertex, computing degree (number of incident edges) is counting size of corresponding list. Number of edges for whole graph is computed in O(n + e) << O(n^2) Number of edges for whole graph is computed in O(n + e) << O(n^2)

Adjacency List for Directed Graph N rows of adjacency matrix (vertices) are represented as n linked lists. N rows of adjacency matrix (vertices) are represented as n linked lists. Nodes in list i are those nodes that one can reach from leaving the corresponding vertex Nodes in list i are those nodes that one can reach from leaving the corresponding vertex Order in linked list doesn’t matter Array of Head Node Pointers

Adjacency List For a directed graph, nodes in a list are those that you can reach leaving from the corresponding vertex For a directed graph, nodes in a list are those that you can reach leaving from the corresponding vertex Computing out degree for any vertex – Count number of nodes in corresponding list Computing out degree for any vertex – Count number of nodes in corresponding list Computing number of total edges in graph: Adding all outdegrees => O(n + e) [visit all head nodes and all items in lists] O(n + e) [visit all head nodes and all items in lists] << O(n^2)

Adjacency List For directed graphs, this approach isn’t very useful for determining in-degree For directed graphs, this approach isn’t very useful for determining in-degree This was trivial in an adjacency matrix (sum down a column) This was trivial in an adjacency matrix (sum down a column) You can build the inverse adjacency list at the same time building adjacency list. You can build the inverse adjacency list at the same time building adjacency list Array of Head Node Pointers

Primitive Graph Operations In essence, can just think of a graph as a container, holding edges and vertices (with a lot of special properties): In essence, can just think of a graph as a container, holding edges and vertices (with a lot of special properties): class Graph {public: Graph(); // create an empty graph void InsertVertex(Vertex v); // Insert v into graph with no incident edges void InsertEdge(Vertex u, Vertex v); // Insert edge (u,v) into graph void DeleteVertex(Vertex v); // Delete v and all edges incident to it void DeleteEdge(Vertex u, Vertex v); Delete edge (u,v) from graph bool IsEmpty(); // if graph has no vertices return true List Adjacent(Vertex v); // return list of all vertices adjacent to vertex v }

Adjacency Matrix/List Construction Note we will be updating these data structures as we call our graph class methods: Note we will be updating these data structures as we call our graph class methods: InsertVertex(Vertex v) InsertVertex(Vertex v) InsertEdge(Vertex u, Vertex v) InsertEdge(Vertex u, Vertex v) DeleteVertex(Vertex v) DeleteVertex(Vertex v) DeleteEdge(Vertex u, Vertex v) DeleteEdge(Vertex u, Vertex v) Linked list approach likely more useful over arrays if don’t know beforehand what adding to graph (how many edges, etc). Linked list approach likely more useful over arrays if don’t know beforehand what adding to graph (how many edges, etc).

Weighted Edges Often see weighted edges on graphs Often see weighted edges on graphs Common uses: Common uses: Distance between vertices Distance between vertices Cost of moving from one vertex to another vertex Cost of moving from one vertex to another vertex Think of vertices as cities – could be real distances or flight costs and want to know shortest/cheapest path Think of vertices as cities – could be real distances or flight costs and want to know shortest/cheapest path How do we incorporate these weights into our graph representations? How do we incorporate these weights into our graph representations?

Weighted Edges Mehran Ilam Sarableh Ivan Could store weights in adjacency matrix (just need a non-zero entry) If using lists, add another field to node

Elementary Graph Operations First Operation: Traversal of graphs First Operation: Traversal of graphs Already saw how this worked in binary trees (inorder, preorder, postorder, depth-order) Already saw how this worked in binary trees (inorder, preorder, postorder, depth-order) Similar idea for general graphs Similar idea for general graphs Given a graph G = (V,E) and a vertex v in V(G), visit all vertices in G that are reachable from v Given a graph G = (V,E) and a vertex v in V(G), visit all vertices in G that are reachable from v This is the subset of the graph that is connected to v. This is the subset of the graph that is connected to v.

Graph Traversal Depth-First Search Depth-First Search Similar to descending down binary tree Similar to descending down binary tree Basic algorithm: Basic algorithm: Begin by starting at vertex v. Begin by starting at vertex v. Select an edge from v, say it’s the edge (v,w). Select an edge from v, say it’s the edge (v,w). Move to vertex w and recurse. Move to vertex w and recurse. When a node has been visited that has all of its adjacent vertices visited, back up to the last node with an unvisited adjacent vertex and recurse. When a node has been visited that has all of its adjacent vertices visited, back up to the last node with an unvisited adjacent vertex and recurse.

DFS Traversal void Graph::DFS() // driver { visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; DFS(0); delete [] visited; } void Graph::DFS(const int v) // workhorse { visited[v] = true; for (each vertex w adjacent to v) //use adjacency matrix or lists if (!visited[w]) DFS(w); }

DFS Traversal – Graph To Traverse

DFS Traversal Order of Traversal: 0, 1, 3 7, 4, 5, 2, 6 Note that performing DFS can find connected components. In this case, the whole graph is connected and thus all nodes were visited.

Analysis of DFS – From a single node Running time dependent on graph structure and representation Running time dependent on graph structure and representation If graph G is represented by adjacency lists If graph G is represented by adjacency lists Determine vertices adjacent to vertex v by following chain of links. Determine vertices adjacent to vertex v by following chain of links. Each node in each lists is visited at most once, and there are 2*e list nodes. Thus, the running time is bounded by the number of edges. Each node in each lists is visited at most once, and there are 2*e list nodes. Thus, the running time is bounded by the number of edges. If graph G is represented by an adjacency matrix If graph G is represented by an adjacency matrix Have to look at n items (n = number of vertices) for each vertex (scanning down each row in the matrix) Have to look at n items (n = number of vertices) for each vertex (scanning down each row in the matrix) Running time is O(n*n). Running time is O(n*n).

Breadth First Traversal Similar to descending across levels of a binary tree. Similar to descending across levels of a binary tree. Visit the starting vertex v. Visit the starting vertex v. Visit all unvisited vertices directly adjacent to v. Visit all unvisited vertices directly adjacent to v. Recurse, visiting all unvisited vertices directly adjacent to those from the previous step. Recurse, visiting all unvisited vertices directly adjacent to those from the previous step.

BFS Traversal Algorithm void Graph::BFS(int v) { visited = new bool[n]; for (int i = 0; i < n; i++) visited[i] = false; visited[v] = true; Queue q; q.insert(v); while (!q.isEmpty()) { v = *q.Delete(v); for (all vertices w adjacent to v) if (!visited[w]) { q.insert(w); visited[w] = true; }} delete [] visited; }

BFS Traversal – Graph to Traverse

BFS Traversal Order of Traversal: 0, 1, 2, 3, 4, 5, 6, 7 Note that performing BFS can find connected components. In this case, the whole graph is connected and thus all nodes were visited.

Analysis of BFS Each visited vertex enters queue exactly once (n vertices). Each visited vertex enters queue exactly once (n vertices). Once in the queue, have to review list of neighbors. Once in the queue, have to review list of neighbors. For adjacency matrix, that list is n items long, meaning the total time is O(n*n) For adjacency matrix, that list is n items long, meaning the total time is O(n*n) For adjacency list, that list has degree(vertex) items, and the sum of the degrees for all n vertices is O(e), so total cost is bounded by number of edges For adjacency list, that list has degree(vertex) items, and the sum of the degrees for all n vertices is O(e), so total cost is bounded by number of edges Same cost as DFS Same cost as DFS

Connected Components How do we find all connected components? How do we find all connected components? Calling BFS or DFS will find a connected component Calling BFS or DFS will find a connected component Those vertices connected to the start node Those vertices connected to the start node To find all connected components, To find all connected components, Select a start vertex, call DFS Select a start vertex, call DFS Select another start vertex which hasn’t been visited by a previous DFS, call DFS Select another start vertex which hasn’t been visited by a previous DFS, call DFS Repeat until all vertices have been visited. Repeat until all vertices have been visited.

Connected Components Algorithm void Graph::Components() { visited = new bool[n]; for (int i = 0; i < n; i++) visited = false; for (i = 0; i < n; i++) { if (!visited[i]) { DFS(i); outputComponent(); } } delete [] visited; }

Analysis of Connected Components Using adjacency lists, Using adjacency lists, Any call to DFS is O(e’) where e’ is the set of edges present in the particular connect component the start node is in. Any call to DFS is O(e’) where e’ is the set of edges present in the particular connect component the start node is in. Sum over e’ has to equal |E|, the total number of edges Sum over e’ has to equal |E|, the total number of edges For loop itself takes O(n) time and calls DFS for non-visited nodes For loop itself takes O(n) time and calls DFS for non-visited nodes DFS not called every time as mark many nodes with each DFS DFS not called every time as mark many nodes with each DFS Total time: O(n+e) Total time: O(n+e) With adjacency matrix, O(n*n) [have to look at all columns for all rows] With adjacency matrix, O(n*n) [have to look at all columns for all rows]

Spanning Trees If a graph G is connected, a DFS or BFS starting at any vertex visits all vertices in G. If a graph G is connected, a DFS or BFS starting at any vertex visits all vertices in G. There are a set of particular edges that are traversed during this process. There are a set of particular edges that are traversed during this process. Let T be the set of edges that are traversed, and N be the remaining edges. Let T be the set of edges that are traversed, and N be the remaining edges.

BFS Traversal Order of Traversal: 0, 1, 2, 3, 4, 5, 6, 7 Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6) (3,7) Not traversed[N]: (4,7), (5,7), (6,7)

A Spanning Tree Algorithm Not hard to record traversed edges in BFS (or DFS) – add to a dynamic list whenever encounter new edge. void Graph::BFS(int v) { visited = new bool[n]; LinkedList tEdgeList = new LinkedList(); for (int i = 0; i < n; i++) visited[i] = false; visited[v] = true; Queue q; q.insert(v); while (!q.isEmpty()) { v = *q.Delete(v); for (all vertices w adjacent to v) if (!visited[w]) { q.insert(w);tEdgeList.append(v,w); visited[w] = true; } } delete [] visited; }

Spanning Trees This set of edges is called a spanning tree for the graph. This set of edges is called a spanning tree for the graph. A spanning tree is any tree that consists solely of edges from G and that includes all vertices in G (the tree “spans” all vertices of the graph). A spanning tree is any tree that consists solely of edges from G and that includes all vertices in G (the tree “spans” all vertices of the graph). Spanning trees are not unique. Spanning trees are not unique. Generated from DFS – called depth-first spanning tree Generated from DFS – called depth-first spanning tree Generated from BFS – called breadth-first spanning tree Generated from BFS – called breadth-first spanning tree

BFS Spanning Tree Order of Traversal: 0, 1, 2, 3, 4, 5, 6, Edges traversed [T]: (0,1), (0,2), (1,3), (1,4), (2,5), (2,6) (3,7) BFS Spanning Tree

DFS Spanning Tree Order of Traversal: 0, 1, 3 7, 4, 5, 2, DFS Spanning Tree Edges traversed [T]: (0,1), (1,3), (3,7), (7,4), (7,5), (5,2), (2,6)

Spanning Trees Spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected Spanning tree is a minimal subgraph G’ of G such that V(G’) = V(G) and G’ is connected Given all vertices from original graph, it is the smallest set of edges one needs for the graph to be connected (to be able to get from any one node to any other) Given all vertices from original graph, it is the smallest set of edges one needs for the graph to be connected (to be able to get from any one node to any other) Any connected graph with n vertices must have n-1 edges or more. Any connected graph with n vertices must have n-1 edges or more. Minimally, every vertex has incoming and outgoing (2n) except last and first (-2) => 2n-2. Every edge is counted twice (as its outgoing and incoming) so divide by 2 => [(2n-2)/2] = n-1 Minimally, every vertex has incoming and outgoing (2n) except last and first (-2) => 2n-2. Every edge is counted twice (as its outgoing and incoming) so divide by 2 => [(2n-2)/2] = n-1 Thus, a spanning tree has exactly n-1 edges (it is the minimal subgraph of G that is connected). Thus, a spanning tree has exactly n-1 edges (it is the minimal subgraph of G that is connected).

Applications of Spanning Trees Very useful in determining optimal connections Very useful in determining optimal connections Example: Communication networks (laying out cable): Assume vertices are cities and edges are communication links between cities. Example: Communication networks (laying out cable): Assume vertices are cities and edges are communication links between cities. All possible spanning trees are all the possible ways you could set up communication links so that everyone could talk to everyone else. All possible spanning trees are all the possible ways you could set up communication links so that everyone could talk to everyone else. Usually interested in finding the cheapest set of links: Requires setting weights (costs) on links and finding “minimum spanning tree” [will see algorithm soon!] Usually interested in finding the cheapest set of links: Requires setting weights (costs) on links and finding “minimum spanning tree” [will see algorithm soon!] Interesting problem – there are potentially many spanning trees? How do you find the minimal efficiently (can’t look at them all!) Interesting problem – there are potentially many spanning trees? How do you find the minimal efficiently (can’t look at them all!)

Finding Minimum Cost Spanning Trees Given a weighted directed graph and a spanning tree for the graph, define the cost of the spanning tree as the sum of the weights of the trees’ edges. Given a weighted directed graph and a spanning tree for the graph, define the cost of the spanning tree as the sum of the weights of the trees’ edges. The minimum cost spanning tree is the tree with minimal cost (the smallest sum over edge weights). The minimum cost spanning tree is the tree with minimal cost (the smallest sum over edge weights). 3 different efficient algorithms for finding minimum spanning tree 3 different efficient algorithms for finding minimum spanning tree

Greedy Algorithms All 3 algorithms are greedy: All 3 algorithms are greedy: Work in stages Work in stages From the set of feasible decisions, make the best decision possible (given some metric of best) for current stage. From the set of feasible decisions, make the best decision possible (given some metric of best) for current stage. Can’t change mind later Can’t change mind later Feasible ensures that the solution will obey problem constraints Feasible ensures that the solution will obey problem constraints Repeat for rest of stages, given the decisions you have already made and what’s left to do. Repeat for rest of stages, given the decisions you have already made and what’s left to do. Best is usually defined as least cost or highest profit. Best is usually defined as least cost or highest profit. Useful in many other programming domains Useful in many other programming domains