Graphs Graph transversals.

Slides:



Advertisements
Similar presentations
CSE Lectures 18 – Graphs Graphs & Characteristics
Advertisements

Review Binary Search Trees Operations on Binary Search Tree
Topological Sort Topological sort is the list of vertices in the reverse order of their finishing times (post-order) of the depth-first search. Topological.
CS 206 Introduction to Computer Science II 03 / 27 / 2009 Instructor: Michael Eckmann.
1 Dijkstra’s Minimum-Path Algorithm Minimum Spanning Tree CSE Lectures 20 – Intro to Graphs.
Graphs Chapter 20 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
Chapter 8, Part I Graph Algorithms.
Data Structures Using C++
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.
Introduction to Graphs
CS 206 Introduction to Computer Science II 11 / 03 / 2008 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 25 / 2009 Instructor: Michael Eckmann.
1 Graphs & Characteristics Graph Representations A Representation in C++ (Ford & Topp) Searching (DFS & BFS) Connected Components Graph G and Its Transpose.
Chapter 9 – Graphs A graph G=(V,E) – vertices and edges
Elementary Graph Algorithms CLRS Chapter 22. Graph A graph is a structure that consists of a set of vertices and a set of edges between pairs of vertices.
Data Structures & Algorithms Graphs. Graph Terminology A graph consists of a set of vertices V, along with a set of edges E that connect pairs of vertices.
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.
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.
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.
© 2006 Pearson Addison-Wesley. All rights reserved 14 A-1 Chapter 14 Graphs.
– Graphs 1 Graph Categories Strong Components Example of Digraph
Main Index Contents 11 Main Index Contents Graph Categories Graph Categories Example of Digraph Example of Digraph Connectedness of Digraph Connectedness.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Introduction to Algorithms
BCA-II Data Structure Using C Submitted By: Veenu Saini
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Graphs A New Data Structure
Graphs Breadth First Search & Depth First Search
Chapter 22 Elementary Graph Algorithms
CS212: Data Structures and Algorithms
Unit 10 Graphs (1) King Fahd University of Petroleum & Minerals
Graphs Representation, BFS, DFS
Introduction to Graphs
Podcast Ch26a Title: Representing Graphs
CSE 2331/5331 Topic 9: Basic Graph Alg.
Csc 2720 Instructor: Zhuojun Duan
Introduction to Graphs
Introduction to Graphs
Common final examinations
Graphs Breadth First Search & Depth First Search
I206: Lecture 15: Graphs Marti Hearst Spring 2012.
CS202 - Fundamental Structures of Computer Science II
CS120 Graphs.
Short paths and spanning trees
CMSC 341 Lecture 21 Graphs (Introduction)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Graph & BFS.
Graphs Representation, BFS, DFS
Graphs Chapter 13.
Graph Representation (23.1/22.1)
What is a Graph? a b c d e V= {a,b,c,d,e} E= {(a,b),(a,c),(a,d),
Chapter 22: Elementary Graph Algorithms
Chapter 11 Graphs.
Spanning Trees Longin Jan Latecki Temple University based on slides by
Richard Anderson Autumn 2016 Lecture 5
Data Structures & Algorithms
Chapter 14 Graphs © 2006 Pearson Addison-Wesley. All rights reserved.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Graph Implementation.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Chapter 16 1 – Graphs Graph Categories Strong Components
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Elementary Graph Algorithms
Introduction to Graphs
Introduction to Graphs
Presentation transcript:

Graphs Graph transversals

Outline Graph Categories Digraph Connectedness of Digraph Adjacency Matrix, Set vertexInfo Object Breadth-First Search Algorithm Depth first Search Algorithm Strong Components Graph G and Its Transpose GT

Graph Terminology A graph G=(V, E) consists of a set V of vertices and a set E of edges that connect pair of vertices. V={v1, v2, …, vn} E={e1, e2, …, em} An edge e E is a pair (vi, vj) A subgraph Gs=(Vs, Es) is a subset of the vertices and edges, where Vs V, Es E Two vertices, vi, vj are adjacent if and only if there is an edge e=(vi, vj) E A path in a graph is a sequence of vertices v0, v1, v2, …, vk such that (vi, vI+1) E Simple Path: each vertex occur only once Cycle: there is a vertex appearing more than once on a path Note 16-1, 16-2

Graph Terminology A graph is connected if each pair of vertices have a path between them A complete graph is a connected graph in which each pair of vertices are linked by an edge

Directed Graphs Graph with ordered edges are called directed graphs or digraphs, otherwise it is undirected. The number of edges that emanate from a vertex v is called the out-degree of v The number of edges that terminate on vertex is called the in-degree of v Note 16-4

Directed Acyclic Graph (DAG) A directed graph that has no cycle is called a directed acyclic graph (DAG) Directed path Directed cycle: a directed path of length 2 or more that connects a vertex to itself A weighted digraph is a directed graph that associates values with the edges. A weight edge e=(vi, vj, wt)

Connectedness of Digraph Strongly connected if for each pair of vertices vi and vj, there is a path P(vi, vj) Weakly connected if for each pair of vertices vi and vj, there is either a path P(vi, vj) or a path P(vj,vi). Note 16-5

The Graph Class Access the properties of a graph Add or delete vertices and edges Update the weight of an edge Identify the list of adjacent vertices

Representation of Graphs Adjacency matrix Adjacent set

Adjacency Matrix An n by n matrix, called an adjacency matrix, identifies the edges. An entry in row i and column j corresponds to the edge e = (vi, vj). Its value is the weight of the edge, or -1 if the edge does not exist.

Adjacency Set For each vertex, an element in the adjacent set is a pair consisting of the adjacent vertex and the weight of the edge.

vertexInfo Object A vertexInfo object consists of seven data members. The first two members, called vtxMapLoc and edges, identify the vertex in the map and its adjacency set.

Vertex Map and Vector vInfo To store the vertices in a graph, we provide a map<T,int> container, called vtxMap, where a vertex name is the key of type T. The int field of a map object is an index into a vector of vertexInfo objects, called vInfo. The size of the vector is initially the number of vertices in the graph, and there is a 1-1 correspondence between an entry in the map and a vertexInfo entry in the vector

VtxMap and Vinfo Example

Graph Traversal Algorithms Breadth-First Visit (Search): visits vertices in the order of their path length from a starting vertex. (generalizes the level-order scan in a binary tree) Depth-First Visit (Search): traverses vertices of a graph by making a series of recursive function calls that follow paths through the graph. (emulate the postorder scan in a binary tree) Finish all my neighbors before me Book 963, figure 16-13 There is no unique order of visits in either BFS or DFS. (selection among the set of neighbors varies) BFS: A,B,C,G,D,E,F DFS (reverse order of processing): A,C,B,D,F,G,E

Breadth-First Search Algorithm BFS: uses a queue to store the vertices awaiting a visit temporally. At each iterative step, the algorithm deletes a vertex from the queue, mark it as visited, and then inserts it into visitSet, the set of visited vertices. The step concludes by placing all unvisited neighbors of the vertex in the queue.

Breadth-First Search… (Cont.)

Breadth-First Search… (Cont.)

Implementation of BFS algorithm uses a queue to store the vertices awaiting a visit temporally. At each iterative step, the algorithm deletes a vertex from the queue, mark it as visited, and then inserts it into visitSet, the set of visited vertices. The step concludes by placing all unvisited neighbors of the vertex in the queue. Each vertex is associate a color from WHITE, GRAY, BLACK. Unvisited: (Initially) WHITE In the process of being searched: (enter into queue) GRAY Visited: (remove from queue) BLACK Time complexity: O(|V|)+O(|E|) Code. Note: 16-10

Depth-First Search Algorithm Book 963 Figure 16-13 *finish all neighbors of D, then finish D) Discovery order: A, B, D, E, F, G, C Finishing order: E, G, F, D, B, C, A

Depth-First Search… (Cont.) B C E D F G book 969, Figure 16-14 for practice exercise Discovery order: A, B, D, G, C, E, F Finishing order: G, D, B, E, F, C, A Discovery order & finishing order?

Implementing the DFS dfsVisit() Includes four arguments: a graph, a WHITE starting vertex, the list of visited vertices in reverse order of finishing times, and Boolean variable for checking cycle Search only vertices that are reachable from the starting vertex dfs() Takes two arguments: a graph and a list Repeatedly call dfsVisit() Time complexity: O(|V|)+O(|E|) book 973, Figure 16-16 for practice exercise book page 974 A as start vertex: Discovery order: A, B, C, D, E, F,G Finishing order: C, B, A, D, G, F, E G as start vertex: Discovery order: G,D,C,A,B,E,F Finishing order: B,A,C,D,F,E dfsList: descending/reverse order of their visit (finish-time) order

Graph Traversal Applications Acyclic graphs Topological sort Strongly connected component

Acyclic graphs A graph is acyclic if it contains no cycles Function acyclic() determine if the graph is acyclic Back edge (v,w): current vertex v and a neighbor vertex w with color gray

Topological sort Topological order: if P(v,w) is a path from v to w, then v must occur before w in the list. If graph is acyclic, defList produce a topological sort of the vertices Implementation, performance, & applications Performance: Note: Toposort 3 Applications (book 977): courses prerequisites, construction projects

Strong Components A strongly connected component of a graph G is a maximal set of vertices SC in G that are mutually accessible. F E C A B D G Components A, B, C D, F, G E

Graph G and Its Transpose GT The transpose has the same set of vertices V as graph G but a new edge set ET consisting of the edges of G but with the opposite direction.

Finding Strong Components Algorithm Step 1. Execute dfs() for graph Step 2. Generate the transpose graph, GT Step 3. Execute a series of dfsVisit( ) calls for vertices using the order of the elements in dfsList obtained in step 1 as the starting vertices. Example: Book 982-983, Fig 16-20, Fig 16-21

Finding Strong Components Example, Verification, Implementation, performance Example: Book 982-983, Fig 16-20, Fig 16-21 dfsList: A B C E D G F SC1={A ,B, C}; SC2={E}, SC3={D, F, G} Verification: book 982-983 Performance: O(V+E)

§- Undirected and Directed Graph (digraph) Summary Slide 1 §- Undirected and Directed Graph (digraph) - Both types of graphs can be either weighted or nonweighted. 30

§- Breadth-First, bfs() Summary Slide 2 §- Breadth-First, bfs() - locates all vertices reachable from a starting vertex - can be used to find the minimum distance from a starting vertex to an ending vertex in a graph. 31

§- Depth-First search, dfs() Summary Slide 3 §- Depth-First search, dfs() - produces a list of all graph vertices in the reverse order of their finishing times. - supported by a recursive depth-first visit function, dfsVisit() - an algorithm can check to see whether a graph is acyclic (has no cycles) and can perform a topological sort of a directed acyclic graph (DAG) - forms the basis for an efficient algorithm that finds the strong components of a graph 32