Data structure for graph algorithms: Adjacent list, Adjacent matrix

Slides:



Advertisements
Similar presentations
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.
Advertisements

Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
Elementary Graph Algorithms Depth-first search.Topological Sort. Strongly connected components. Chapter 22 CLRS.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
More Graphs COL 106 Slides from Naveen. Some Terminology for Graph Search A vertex is white if it is undiscovered A vertex is gray if it has been discovered.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Traversals Visit vertices of a graph G to determine some property: Is G connected? Is there a path from vertex a to vertex b? Does G have a cycle?
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
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.
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
Lecture 11 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Chapter 22 Elementary Graph Algorithms. 2 Introduction G=(V, E) –V = vertex set –E = edge set Graph representation –Adjacency list –Adjacency matrix.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graphs & Paths Presentation : Part II. Graph representation Given graph G = (V, E). May be either directed or undirected. Two common ways to represent.
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.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
CS138A Elementary Graph Algorithms Peter Schröder.
Representing Graphs Depth First Search Breadth First Search Graph Searching Algorithms.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
Data Structures, Algorithms & Complexity
CSC317 Graph algorithms Why bother?
Topological Sort Minimum Spanning Tree
Lecture 12 Graph Algorithms
Introduction to Graphs
CS200: Algorithm Analysis
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Graph Representation, DFS and BFS
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill.
Binhai Zhu Computer Science Department, Montana State University
Graphs Graph transversals.
Elementary Graph Algorithms
CS 3343: Analysis of Algorithms
Graphs A graph G = (V, E) V = set of vertices, E = set of edges
Intro to Graph Algorithms (Slides originally created by David Luebke)
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Graphs Representation, BFS, DFS
BFS,DFS Topological Sort
Graphs Chapter 15 explain graph-based algorithms Graph definitions
BFS,DFS Topological Sort
Advanced Algorithms Analysis and Design
Graph Theory and Representation
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Basic Graph Algorithms
Chapter 11 Graphs.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms Searching in a Graph.
Graph Implementation.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
A vertex u is reachable from vertex v iff there is a path from v to u.
CMSC 341 Graphs 2.
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Chapter 9: Graphs Shortest Paths
Presentation transcript:

Data structure for graph algorithms: Adjacent list, Adjacent matrix b c d e f Adjacent lists a d e c b f Adjacent matrix

Adjacent list: Each vertex u has an adjacent list Adj[u] (1) For a connected graph G, if G is directed graph the total size of all the adjacent lists is |E|, and if G is undirected graph then the total size of all the adjacent lists is 2|E|. Generally, the total size of adjacent lists is O(V+E). (2) For a weighted graph G, weight w(u,v) of edge (u,v) is kept in Adj[u] with vertex v. Adjacent matrix : Each vertex is given a number from 1,2,…,|V|. (1) For a undirected graph, its adjacent matrix is symmetric. (2) For a weighted graph, weight w(u,v) is kept in its adjacent matrix at row i and column j.

Comparison between adjacent list and adjacent matrix 1 2 5 4 3 3 4 2 1 5 1 2 3 4 5 1 0 1 0 0 1 2 1 0 1 1 1 3 0 1 0 1 0 4 0 1 1 0 1 5 1 1 0 1 0 1 2 4 5 3 6 2 1 1 2 3 4 5 6 1 0 1 0 1 0 0 2 0 0 0 0 1 0 3 0 0 0 0 1 1 4 0 1 0 0 0 0 5 0 0 0 1 0 0 6 0 0 0 0 0 1 Comparison between adjacent list and adjacent matrix If |E| is much smaller than then adjacent list is better (using less memory). It costs time using adjacent lists to find if v is adjacent to u.

Given G = (V,E) and vertex s, search all the vertices that s can arrive. Breadth-first search (BFS): Searching the vertices whose distance from s is k ealier than visiting those whose distance from s is k+1. 0 1 8 w Q r 1 1 u r s t v w x y (2) s Q 0 8 u r s t v w x y (1) 0 1 8 2 u r s t v w x y 1 2 2 Q (3) 0 1 2 8 u r s t v w x y 2 2 2 Q (4) r 0 1 2 8 3 u s t v w x y 2 2 3 Q (5) 2 3 3 0 1 2 3 u r s t v w x y Q (6) 0 1 2 3 u r s t v w x y Q 3 3 (7) t s u 0 1 2 3 r v w x y Q (8) 0 1 2 3 u r s t v w x y Q: (8)

Analysis of the algorithm Each vertex is put into queue Q at most once. Therefore, the number of operation for the queue is O(|V|). Each adjacent list is at most scanned once. Therefore, the total running time for scanning adjacent lists is O(|E|). The running time for initiation is O(|V|). Therefore, the total running time of the algorithm is O(|V|+|E|).

Find the path from s to v in BSF

Depth-first search: Search deeper in the graph whenever possible. (1) Each vertex has two timestamps: d[v] is the first timestamp when v is first discovered, and f[v] is the second timestamps when the search finishes examining v’s adjacent list. (2) It generates a number of depth-first search trees. u v w 1/ x y z (a) u v w 1/ 2/ x y z (b) u v w 1/ 2/ 3/ x y z (c) u v w 1/ 2/ 4/ 3/ x y z (d) u v w 1/ 2/ 4/ 3/ x y z (e) B u v w 1/ 2/ 4/5 3/ x y z (f) B u v w 1/ 2/ 4/5 3/6 x y z (g) B u v w 1/ 2/7 4/5 3/6 x y z (h) B u v w 1/8 2/7 4/5 3/6 x y z (i) B F u v w 1/8 2/7 4/5 3/6 x y z (j) B F (k) u v w 1/8 2/7 9/ 4/5 3/6 x y z B F (l) u v w 1/8 2/7 9/ 4/5 3/6 x y z B F C (m) u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z B F C (n) u v w 1/8 2/7 9/ 4/5 3/6 10/ x y z B F C (o) u v w 1/8 2/7 9/ 4/5 3/6 10/11 x y z B F C (p) u v w 1/8 2/7 9/12 4/5 3/6 10/11 x y z B F C

Running time (1) The running time except DFS-VIST is O(|V|). (2) Each vertex is called by DFS-VISIT only once, because only white vertices will be called by DES-VISIT and when they are called their color is changed to gray immediately. (3) The loop in DFS-VISIT is executed only |Adj[v]| times. Therefore, the total running time of the algorithm is O(|V|+|E|).