Csc 2720 Instructor: Zhuojun Duan

Slides:



Advertisements
Similar presentations
Graphs COP Graphs  Train Lines Gainesville OcalaDeltona Daytona Melbourne Lakeland Tampa Orlando.
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 excerpts Graphs (breadth-first-search)
CS 206 Introduction to Computer Science II 11 / 11 / Veterans Day Instructor: Michael Eckmann.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.
Graphs Chapter 12. Chapter 12: Graphs2 Chapter Objectives To become familiar with graph terminology and the different types of graphs To study a Graph.
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
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.
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.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
© 2006 Pearson Addison-Wesley. All rights reserved14 A-1 Chapter 14 Graphs.
Chapter 14 Graphs. © 2004 Pearson Addison-Wesley. All rights reserved Terminology G = {V, E} A graph G consists of two sets –A set V of vertices,
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Graphs.
CISC 235: Topic 9 Introduction to Graphs. CISC 235 Topic 92 Outline Graph Definition Terminology Representations Traversals.
1 Subgraphs A subgraph S of a graph G is a graph such that The vertices of S are a subset of the vertices of G The edges of S are a subset of the edges.
Graphs. Graphs Similar to the graphs you’ve known since the 5 th grade: line graphs, bar graphs, etc., but more general. Those mathematical graphs are.
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.
Graphs Upon completion you will be able to:
Graphs. Contents Terminology Graphs as ADTs Applications of Graphs.
Backtracking Algorithm Depth-First Search Text Read Weiss, § 9.6 Depth-First Search and § 10.5 Backtracking Algorithms.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved14 B-1 Chapter 14 (continued) Graphs.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs Chapter 20.
CS202 - Fundamental Structures of Computer Science II
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Data Structures Graphs - Terminology
Data Structures 13th Week
CS202 - Fundamental Structures of Computer Science II
Common final examinations
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Comp 245 Data Structures Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Graphs.
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 11 Graphs.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013
Spanning Trees Longin Jan Latecki Temple University based on slides by
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Depth-First Search CSE 2011 Winter April 2019.
Graphs Chapter 7 Visit for more Learning Resources.
Graph Implementation.
Depth-First Search CSE 2011 Winter April 2019.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
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:

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