Graphs I Kruse and Ryba Chapter 12. Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected.

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Comp 122, Fall 2004 Elementary Graph Algorithms. graphs Lin / Devi Comp 122, Fall 2004 Graphs  Graph G = (V, E) »V = set of vertices »E = set of.
Graph A graph, G = (V, E), is a data structure where: V is a set of vertices (aka nodes) E is a set of edges We use graphs to represent relationships among.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 12 Graphs.
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 Graph  Types of Graphs Types of Graphs  Data Structures to Store Graphs Data Structures to Store Graphs  Graph Definitions Graph Definitions.
Graph.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 21: Graphs.
Graph & BFS.
Chapter 9 Graph algorithms. Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Introduction to Graphs
Introduction to Graphs What is a Graph? Some Example applications of Graphs. Graph Terminologies. Representation of Graphs. –Adjacency Matrix. –Adjacency.
Spring 2010CS 2251 Graphs Chapter 10. Spring 2010CS 2252 Chapter Objectives To become familiar with graph terminology and the different types of graphs.
Chapter 9 Graph algorithms Lec 21 Dec 1, Sample Graph Problems Path problems. Connectedness problems. Spanning tree problems.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
Graphs Chapter 28 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
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.
1 Abstract Data Type We have discussed: List Tree Today we will talk about Graph.
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
Lecture 13 Graphs. Introduction to Graphs Examples of Graphs – Airline Route Map What is the fastest way to get from Pittsburgh to St Louis? What is the.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Computer Science 112 Fundamentals of Programming II Introduction to Graphs.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
 What is a graph? What is a graph?  Directed vs. undirected graphs Directed vs. undirected graphs  Trees vs graphs Trees vs graphs  Terminology: Degree.
Graphs. 2 Graph definitions There are two kinds of graphs: directed graphs (sometimes called digraphs) and undirected graphs Birmingham Rugby London Cambridge.
Graph Introduction, Searching Graph Theory Basics - Anil Kishore.
Data Structures & Algorithms Graphs Richard Newman based on book by R. Sedgewick and slides by S. Sahni.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 10 Prepared by İnanç TAHRALI.
Graphs Upon completion you will be able to:
Graph. Graph Usage I want to visit all the known famous places starting from Seoul ending in Seoul Knowledge: distances, costs Find the optimal(distance.
Shahed University Dr. Shahriar Bijani May  A path is a sequence of vertices P = (v 0, v 1, …, v k ) such that, for 1 ≤ i ≤ k, edge (v i – 1, v.
1 Algorithms CSCI 235, Fall 2015 Lecture 32 Graphs I.
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.
Graphs. Graph Definitions A graph G is denoted by G = (V, E) where  V is the set of vertices or nodes of the graph  E is the set of edges or arcs connecting.
Graph Representations And Traversals. Graphs Graph : – Set of Vertices (Nodes) – Set of Edges connecting vertices (u, v) : edge connecting Origin: u Destination:
Graph Searching CSIT 402 Data Structures II. 2 Graph Searching Methodology Depth-First Search (DFS) Depth-First Search (DFS) ›Searches down one path as.
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
Graph Theory Def: A graph is a set of vertices and edges G={V,E} Ex. V = {a,b,c,d,e} E = {ab,bd,ad,ed,ce,cd} Note: above is a purely mathematical definition.
Lecture #13. Topics 1.The Graph Abstract Data Type. 2.Graph Representations. 3.Elementary Graph Operations.
1 Data Structures and Algorithms Graphs. 2 Graphs Basic Definitions Paths and Cycles Connectivity Other Properties Representation Examples of Graph Algorithms:
Subject Four Graphs Data Structures. What is a graph? A data structure that consists of a set of nodes (vertices) and a set of edges that relate the nodes.
CS 201: Design and Analysis of Algorithms
Elementary Graph Algorithms
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Csc 2720 Instructor: Zhuojun Duan
Graph Operations And Representation
Graph.
Elementary Graph Algorithms
Graphs Representation, BFS, DFS
BFS,DFS Topological Sort
Graphs.
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graph Implementation.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Richard Anderson Winter 2019 Lecture 6
Richard Anderson Winter 2019 Lecture 5
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
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 I Kruse and Ryba Chapter 12

Undirected Graphs An undirected graph is a set of nodes and a set of edges between nodes; e.g.: The graph is undirected because its edges do not have a particular direction.

Edges We sometimes need to refer to the edges e i of a graph: e0e0 e1e1 e3e3 e4e4 e5e5 e2e2

Vertices We also need to refer to the vertices v j of a graph: v1v1 e0e0 e1e1 e3e3 e4e4 e5e5 e2e2 v2v2 v3v3 v4v4 v0v0

Undirected Graph Definition An undirected graph G = (V, E) is a finite set of vertices V, together with a finite set of edges E. Both sets might be empty (no vertices and no edges), which is called the empty graph. Each edge has associated with it two vertices. If two vertices are connected by an edge, they are said to be adjacent.

Undirected Graph Example: Subway Map

Another Undirected Graph: vBNS (fast internet backbone)

Directed Graphs In a directed graph each vertex has an associated direction; e.g.: Each edge is associated with two vertices: its source vertex and target vertex.

Directed Graph Examples Flow graph: vertices are pumping stations and edges are pipelines connecting them. Task network: each project has a number of component activities called tasks. Each task has a duration (amount of time needed to complete task).

Loops A loop is an edge that connects a vertex with itself; for example: Loops can occur in directed graphs or undirected graphs.

Multiple Edges In principle, a graph may have two or more edges connecting the same two vertices in the same direction: These are called multiple edges. In a graph diagram, each edge is drawn separately.

Simple Graphs A graph is said to be a simple graph, if: 1.the graph has no loops, and 2.no multiple edges

Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0

Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0 Example path: v 0, v 3, v 4

Path A path in a graph is a sequence of vertices, v 0, v 1, … v m, such that each adjacent pair of vertices in the list are connected by an edge. v1v1 v2v2 v3v3 v4v4 v0v0 Example path: v 0, v 3, v 4 v 0, v 3, v 2, v 1, v 4

Path In a directed graph, the connection must go from source v i to target v i+1. v1v1 v2v2 v3v3 v4v4 v0v0 Is there a path from v 0 to v 4 ? v 3 to v 1 ? v 1 to v 2 ? v 2 to v 1 ?

Example Task Network Start Find a realtor Look at homes Choose a home to buy Apply for financing and establish price limit Investigate neighborhood schools Prepare offer Investigate termite and soil reports Present offer How long will it take to buy a house? (The duration of each task is shown below each task).

Critical Path Start Find a realtor Look at homes Choose a home to buy Apply for financing and establish price limit Investigate neighborhood schools Prepare offer Investigate termite and soil reports Present offer The task will take as long as the maximum duration path from start to finish.

Cycles A cycle in a graph is a path that leads back to the first node in the path. v1v1 v2v2 v3v3 v4v4 v0v0 Example cycles: v 1, v 2, v 3, v 2, v 1

Cycles A cycle in a graph is a path that leads back to the first node in the path. v1v1 v2v2 v3v3 v4v4 v0v0 Example cycles: v 1, v 2, v 3, v 2, v 1 v 0, v 3, v 4, v 1, v 0

Does this graph have any cycles?

Cycles in Directed Graphs A cycle in a directed graph must follow edge directions; e.g.:

DAG: Directed Acyclic Graph A directed acyclic graph (DAG) must have no cycles; e.g.: In other words, there is no path that starts and ends at the same node.

Complete Graph An undirected graph which has an edge between every pair of vertices.

Connected Graph An undirected graph which has a path between every pair of vertices

Strongly Connected Graph A directed graph which has a path between every pair of vertices.

Weakly Connected Graph A directed graph which is connected, but not not strongly connected.

A DAG that is not Connected

Connected Components

Review of Terms Undirected vs. directed graphs Multiple edges, loops Simple graph Path Cycle Directed, acyclic graph (DAG) Connected, strongly connected, and weakly connected graphs Connected component

Graph Implementations

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

Adjacency Matrix

C++ Adjacency Matrix bool graph_adjacency[4][4];

Adjacency Matrix: Weighted Edges double graph_adjacency[4][4];

Weighted Edges: An Example $320 $245 San Fransisco Boston Chicago $452 $60 $180 Dallas Flights. What’s the cheapest way to fly San Fransisco  Boston?

Weighted Edges: An Example San Fransisco Boston Chicago Dallas Mileage for various hops from San Fransisco  Boston* *Mileage is not real, but for sake of example.

Weighted Edges: An Example Fran Pat ADSL provider 100Kbs Available internet bandwidth on various paths between an ADSL provider and customers*. *This is a simplication for sake of example, and bit rates are fictional. 1Gbs 100Kbs 500Kbs

Adjacency Matrix: Undirected Graph bool graph_adjacency[4][4];

Adjacency Matrix: Undirected Graph No loops  zero diagonal.

Adjacency Matrix: Undirected Graph

Adjacency Matrix: Undirected Graph

Adjacency Matrix: Undirected Graph

Adjacency Matrix: Undirected Graph

Symmetry of Adjacency Matrix of Undirected Graph a[i][j]== a[j][i]

Non-symmetric Adjacency Matrix for Directed Graph a[i][j]  a[j][i]

Symmetric Adjacency Matrix An undirected graph will always have a symmetric adjacency matrix. A directed graph may not have a symmetric adjacency matrix. If a directed graph has a symmetric matrix, what does that mean?

Non-symmetric Adjacency Matrix Unweighted Directed Graph a[i][j]  a[j][i]

Add an Edge a[i][j]=a[j][i]

Adjacency Matrix for Weighted Directed Graph a[i][j]=a[j][i]

Time Complexity Analysis Given N we have vertices in a graph, and we use the adjacency matrix representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Representing Graphs with Edge Lists Null Edge list for vertex 0 Null Edge list for vertex 1 0 Edge list for vertex 2 1 Null Edge list for vertex 3 Null

Time Complexity Analysis Given N we have vertices in a graph, and we use the edge list representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Representing Graphs with Edge Sets Assume a Set template class is available: Declare array of sets, one per node in graph: Set connections[N]; Each node’s set contains the indices of all vertices that it is connected to.

Time Complexity Analysis Given N we have vertices in a graph, and we use the edge set representation. 1.What is the worst case and average complexity of inserting an edge in the graph? 2.What is the worst case and average complexity of removing an edge in the graph? 3.What is the complexity of retrieving a list of all nodes that share and edge with a particular vertex?

Graph Implementations Summary Adjacency matrix Edge lists Edge sets

Graph Searching

Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; }

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } s Q = 

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } s Q = 

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } s Q = 0

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 0

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 012

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 0123

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 0123

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 0123

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 01234

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q =

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q =

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q =

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q =

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q =

Breadth First Algorithm Given graph G=(V,E) and source vertex s  V Create a queue Q For each vertex u  V – {s} color[u]  white color[s]  gray Q  {s} While Q   { u  head[Q]; for each v  Adjacent[u] if color[v] = white { color[v]  gray Enqueue(Q,v) } Dequeue(Q) color[u]  black; } u Q = 

Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } s S = 

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } s S = 

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } s S =  0

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S = 

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  1

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  321

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  321

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  321

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  321

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  3215

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  3215

3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  521

3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  5214

3 Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  5214

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  43521

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  43521

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  43521

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S =  43521

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S = 

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S = 

Graph Search Choice of container –If a stack is used as the container for adjacent vertices, we get depth first search. –If a list is used as the container adjacent vertices, we get breadth first search.

Depth First Algorithm Given graph G=(V,E) and source vertex s  V Create a stack S For each vertex u  V – {s} color[u]  white color[s]  gray S  {s} While S   { u = Pop(S) for each v  Adjacent[u] if color[v] = white { color[v]  gray Push(S,v) } color[u]  black; } u S = 