Csc 2720 Instructor: Zhuojun Duan Data structure Graph Csc 2720 Instructor: Zhuojun Duan
Outline
Outline Definition of graph Variations of graph Implementation Adjacency matrix Adjacency list Traversal of graph
Definition
Terminology G = {V, E} A graph G consists of two sets A subgraph A set V of vertices, or nodes A set E of edges A subgraph Consists of a subset of a graph’s vertices and a subset of its edges Adjacent vertices Two vertices that are joined by an edge
Terminology A path between two vertices A simple path A cycle A sequence of edges that begins at one vertex and ends at another vertex May pass through the same vertex more than once A simple path A path that passes through a vertex only once A cycle A path that begins and ends at the same vertex A simple cycle A cycle that does not pass through a vertex more than once
Graph variations A connected graph A disconnected graph A graph that has a path between each pair of distinct vertices A disconnected graph A graph that has at least one pair of vertices without a path between them A complete graph A graph that has an edge between each pair of distinct vertices
Graph variations Multigraph Not a graph Allows multiple edges between vertices
Graph variations Weighted graph A graph whose edges have numeric labels
Graph variations Undirected graph Edges do not indicate a direction Directed graph, or diagraph Each edge is a directed edge Can have two edges between a pair of vertices, one in each direction Directed path A sequence of directed edges between two vertices Vertex y is adjacent to vertex x if There is a directed edge from x to y
Outline Definition of graph Variations of graph Implementation Adjacency matrix Adjacency list Traversal of graph
Operations in a graph Graphs can be used as abstract data types Two options for defining graphs Vertices contain values Vertices do not contain values Operations of the ADT graph Create an empty graph Determine whether a graph is empty Determine the number of vertices in a graph Determine the number of edges in a graph
Operations in a graph Operations of the ADT graph (Continued) Determine whether an edge exists between two given vertices; for weighted graphs, return weight value Insert a vertex in a graph whose vertices have distinct search keys that differ from the new vertex’s search key Insert an edge between two given vertices in a graph Delete a particular vertex from a graph and any edges between the vertex and other vertices Delete the edge between two given vertices in a graph Retrieve from a graph the vertex that contains a given search key
Implementing Graphs Most common implementations of a graph Adjacency matrix Adjacency list Adjacency matrix for a graph with n vertices numbered 0, 1, …, n – 1 An n by n array matrix such that matrix[i][j] is 1 (or true) if there is an edge from vertex i to vertex j 0 (or false) if there is no edge from vertex i to vertex j
Implementing Graphs a) A directed graph and b) its adjacency matrix
Implementing Graphs Adjacency matrix for a weighted graph with n vertices numbered 0, 1, …, n – 1 An n by n array matrix such that matrix[i][j] is The weight that labels the edge from vertex i to vertex j if there is an edge from i to j if there is no edge from vertex i to vertex j Diagonal Symmetric matrix a) A weighted undirected graph and b) its adjacency matrix
Outline Definition of graph Variations of graph Implementation Adjacency matrix Adjacency list Traversal of graph
Implementing Graphs Adjacency list An adjacency list for a graph with n vertices numbered 0, 1, …, n – 1 Consists of n linked lists The ith linked list has a node for vertex j if and only if the graph contains an edge from vertex i to vertex j This node can contain either Vertex j’s value, if any An indication of vertex j’s identity
Implementing Graphs a) A directed graph and b) its adjacency list © 2011 Pearson Addison-Wesley. All rights reserved Implementing Graphs a) A directed graph and b) its adjacency list
Implementing Graphs Adjacency list for an undirected graph Treats each edge as if it were two directed edges in opposite directions a) A weighted undirected graph and b) its adjacency list
Implementing Graphs Adjacency matrix compared with adjacency list Two common operations on graphs 1- Determine whether there is an edge from vertex i to vertex j 2- Find all vertices adjacent to a given vertex i Adjacency matrix Supports operation 1 more efficiently Adjacency list Supports operation 2 more efficiently Often requires less space than an adjacency matrix
Outline Definition of graph Variations of graph Implementation Adjacency matrix Adjacency list Traversal of graph
Graph Traversals A graph-traversal algorithm Visits all the vertices that it can reach Visits all vertices of the graph if and only if the graph is connected A connected component The subset of vertices visited during a traversal that begins at a given vertex Can loop indefinitely if a graph contains a loop To prevent this, the algorithm must Mark each vertex during a visit, and Never visit a vertex more than once
Graph Traversals Search vs Traversal Search: Look for a given node stop when node found, even if not all nodes were visited Traversal: Always visit all nodes Two way of Traversal for graph Depth-first Search (DFS) Breadth-first Search (BFS)
Graph Traversals if unvisited Pseudo-Code for Depth-First Search depth-first-search mark vertex x as visited for each adjacent vertex of x if unvisited do a depth-first search on adjacent vertex
First example of DFS
Depth-First Search for undirected graph B C D E F G
Depth-First Search for undirected graph v A B C D E F G A
Depth-First Search for undirected graph v A B C D E F G A
Depth-First Search for undirected graph v A v B C D E F G A B
Depth-First Search for undirected graph v A v B C D E F G A B
Depth-First Search for undirected graph v A v B C D E F G A B
Depth-First Search for undirected graph v A v B C v D E F G A B D
Depth-First Search for undirected graph v A v B C v D E F G A B D
Depth-First Search for undirected graph v A v B C v D E F G A B D
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G A B D E
Depth-First Search for undirected graph v A v B C v D E v F G v A B D E F
Depth-First Search for undirected graph v A v B C v D E v F G v A B D E F
Depth-First Search for undirected graph v A v B C v D E v F G v A B D E F
Depth-First Search for undirected graph v A v v B C v D E v F G v A B D E F C
Depth-First Search for undirected graph v A v v B C v D E v F G v A B D E F C
Depth-First Search for undirected graph v A v v B C v D E v F G v A B D E F C
Depth-First Search for undirected graph v A v v B C v D E v F G v A B D E F C
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph v A v v B C v v D E v F G v A B D E F C G
Depth-First Search for undirected graph B C D E F G A B D E F C G
Second example of DFS
Task: Conduct a depth-first search of the graph starting with node D DFS for directed graph by Stack Visited Array F C A B C D E F G H A B D H G E Task: Conduct a depth-first search of the graph starting with node D
DFS for directed graph by Stack Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Visit D
DFS for directed graph by Stack Visited Array F C A B C D √ E F G H D A B D H G E The order nodes are visited: D Consider nodes adjacent to D, decide to visit C first (Rule: visit adjacent nodes in alphabetical order)
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C Visit C
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H C D A B D H G E The order nodes are visited: D, C No nodes adjacent to C; cannot continue backtrack, i.e., pop stack and restore previous state
Back to D – C has been visited, decide to visit E next DFS for directed graph by Stack Visited Array F C A B C √ D E F G H D A B D H G E The order nodes are visited: D, C Back to D – C has been visited, decide to visit E next
Back to D – C has been visited, decide to visit E next DFS for directed graph by Stack Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Back to D – C has been visited, decide to visit E next
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H E D A B D H G E The order nodes are visited: D, C, E Only G is adjacent to E
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Visit G
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G Nodes D and H are adjacent to G. D has already been visited. Decide to visit H.
DFS for directed graph by Stack Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Visit H
Nodes A and B are adjacent to F. Decide to visit A next. DFS for directed graph by Stack Visited Array F C A B C √ D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H Nodes A and B are adjacent to F. Decide to visit A next.
DFS for directed graph by Stack Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Visit A
Only Node B is adjacent to A. Decide to visit B next. DFS for directed graph by Stack Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A Only Node B is adjacent to A. Decide to visit B next.
DFS for directed graph by Stack Visited Array F C A √ B C D E F G H B A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B Visit B
No unvisited nodes adjacent to B. Backtrack (pop the stack). DFS for directed graph Visited Array F C A √ B C D E F G H A H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to B. Backtrack (pop the stack).
No unvisited nodes adjacent to A. Backtrack (pop the stack). DFS for directed graph Visited Array F C A √ B C D E F G H H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to A. Backtrack (pop the stack).
No unvisited nodes adjacent to H. Backtrack (pop the stack). DFS for directed graph Visited Array F C A √ B C D E F G H G E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to H. Backtrack (pop the stack).
No unvisited nodes adjacent to G. Backtrack (pop the stack). DFS for directed graph Visited Array F C A √ B C D E F G H E D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to G. Backtrack (pop the stack).
No unvisited nodes adjacent to E. Backtrack (pop the stack). DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B No unvisited nodes adjacent to E. Backtrack (pop the stack).
F is unvisited and is adjacent to D. Decide to visit F next. DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B F is unvisited and is adjacent to D. Decide to visit F next.
DFS for directed graph The order nodes are visited: Visited Array F C A √ B C D E F G H F D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Visit F
No unvisited nodes adjacent to F. Backtrack. DFS for directed graph Visited Array F C A √ B C D E F G H D A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to F. Backtrack.
No unvisited nodes adjacent to D. Backtrack. DFS for directed graph Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F No unvisited nodes adjacent to D. Backtrack.
Stack is empty. Depth-first traversal is done. DFS for directed graph Visited Array F C A √ B C D E F G H A B D H G E The order nodes are visited: D, C, E, G, H, A, B, F Stack is empty. Depth-first traversal is done.
Graph Traversals put root node onto a queue Pseudo-Code for Breadth-First Search (BFS) breadth-first-traversal put root node onto a queue while the queue is not empty dequeue a node from the queue visit the node x e.g., print value enqueue the children of x
First example of BFS
Breadth-First Search for undirected graph Queue: A B C Current: D E F G A B C D E F G
Breadth-First Search for undirected graph Queue: A A B C Current: D E F G
Breadth-First Search for undirected graph Queue: A A B C Current: D E F G A
Breadth-First Search for undirected graph Queue: A B C Current: D E F G A A
Breadth-First Search for undirected graph Queue: A B B C Current: D E F G A A
Breadth-First Search for undirected graph Queue: A C B B C Current: D E F G A A
Breadth-First Search for undirected graph Queue: A C B B C Current: D E F G B A
Breadth-First Search for undirected graph Queue: A C B C Current: D E F G B A B
Breadth-First Search for undirected graph Queue: A D C B C Current: D E F G B A B
Breadth-First Search for undirected graph Queue: A E D C B C Current: D E F G B A B
Breadth-First Search for undirected graph Queue: A E D C B C Current: D E F G C A B
Breadth-First Search Queue: A E D B C Current: D E F G C A B C
Breadth-First Search for undirected graph Queue: A F E D B C Current: D E F G C A B C
Breadth-First Search for undirected graph Queue: A G F E D B C D E F G Current: C A B C
Breadth-First Search for undirected graph Queue: A G F E D B C D E F G Current: D A B C
Breadth-First Search for undirected graph Queue: A G F E B C D E F G Current: D A B C D
Breadth-First Search for undirected graph Queue: A G F E B C D E F G Current: E A B C D
Breadth-First Search for undirected graph Queue: A G F B C D E F G Current: E A B C D E
Breadth-First Search for undirected graph Queue: A G F B C D E F G Current: F A B C D E
Breadth-First Search for undirected graph Queue: A G B C D E F G Current: F A B C D E F
Breadth-First Search for undirected graph Queue: A G B C D E F G Current: G A B C D E F
Breadth-First Search for undirected graph Queue: A B C D E F G Current: G A B C D E F G
Breadth-First Search for undirected graph A B C D E F G
Second example of BFS
Breadth-First Search for directed graph Breadth-first search starts with given node F C A B D H G E Task: Conduct a breadth-first search of the graph starting with node D
Breadth-First Search for undirected graph Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D
Breadth-First Search for undirected graph Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C
Breadth-First Search for undirected graph Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C, E
Breadth-First Search for undirected graph Breadth-first search starts with given node Then visits nodes adjacent in some specified order (e.g., alphabetical) Like ripples in a pond F C A B D H G E 1 Nodes visited: D, C, E, F
Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples F C A B D H G E 2 1 Nodes visited: D, C, E, F, G
Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H
Nodes visited: D, C, E, F, G, H, A Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples 4 F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H, A
Nodes visited: D, C, E, F, G, H, A, B Breadth-First Search for undirected graph When all nodes in ripple are visited, visit nodes in next ripples 4 F C A B D H 3 G E 2 1 Nodes visited: D, C, E, F, G, H, A, B
© 2011 Pearson Addison-Wesley. All rights reserved Graph Traversals Visitation order for a) a depth-first search; b) a breadth-first search