Data Structures, Algorithms & Complexity

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

1 Graphs Traversals In many graph problems, we need to traverse the vertices of the graph in some order Analogy: Binary tree traversals –Pre-order Traversal.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graphs Breadth First Search & Depth First Search by Shailendra Upadhye.
Graphs II Kruse and Ryba Chapter 12. Undirected Graph Example: Subway Map.
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?
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?
Lecture 14: Graph Algorithms Shang-Hua Teng. Undirected Graphs A graph G = (V, E) –V: vertices –E : edges, unordered pairs of vertices from V  V –(u,v)
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
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.
Spring 2015 Lecture 10: Elementary Graph Algorithms
Sept Elementary Graph Algorithms Graph representation Graph traversal -Breadth-first search -Depth-first search Parenthesis theorem.
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.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
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.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
Graph. Graphs G = (V,E) V is the vertex set. Vertices are also called nodes and points. E is the edge set. Each edge connects two different vertices.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs Breadth First Search & Depth First Search
CS 201: Design and Analysis of Algorithms
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
Graphs Representation, BFS, DFS
CSE 373 Topological Sort Graph Traversals
CSC317 Graph algorithms Why bother?
CC 215 Data Structures Graph Searching
Depth-First Search.
Graphs Breadth First Search & Depth First Search
Graph: representation and traversal CISC4080, Computer Algorithms
Graph.
Graph Representation, DFS and BFS
Binhai Zhu Computer Science Department, Montana State University
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
Graph & BFS.
Finding Shortest Paths
Graphs Representation, BFS, DFS
BFS,DFS Topological Sort
Graphs Chapter 15 explain graph-based algorithms Graph definitions
Breadth-First Search The Breadth-first search algorithm
Chapter 22: Elementary Graph Algorithms I
Graph Representation (23.1/22.1)
Elementary Graph Algorithms
Graphs.
Basic Graph Algorithms
Chapter 22: Elementary Graph Algorithms
Connected Components, Directed Graphs, Topological Sort
CSE 373 Data Structures Lecture 16
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Algorithms Searching in a Graph.
GRAPHS G=<V,E> Adjacent vertices Undirected graph
Data structure for graph algorithms: Adjacent list, Adjacent matrix
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Algorithms CSCI 235, Spring 2019 Lecture 32 Graphs I
Presentation transcript:

Data Structures, Algorithms & Complexity GRIFFITH COLLEGE DUBLIN Data Structures, Algorithms & Complexity Elementary Graph Algorithms Lecture 16

A Graph A graph can be thought of a collection of vertices (V) and edges (E), so we write, G = (V, E) Graphs can be directed, or undirected, weighted or unweighted. A directed graph, or digraph, is a graph where the edge set is an ordered pair. That is, edge 1 being connected to edge 2 does not imply that edge 2 is connected to edge 1. An undirected graph is a graph where the edge set in an unordered pair. That is, edge 1 being connected to edge 2 does imply that edge 2 is connected to edge 1. Lecture 16

A Weighted Graph A weighted graph is graph which has a value associated with each edge. This can be a distance, or cost, or some other numeric value associated with the edge. (a) A directed graph (b) An undirected graph 1 2 3 1 2 3 4 5 6 4 5 6 (c) A weighted graph 1 2 3 4 5 6 2 5 2 1 4 3 Lecture 16

Representing a Graph There are two standard ways to represent a graph G = (V,E) in computer science As a collection of adjacency lists As an adjacency matrix Take the following graph 1 2 3 4 5 We can represent this in two ways Lecture 16

Adjacency Lists & matrices As an adjacency list this is As an adjacency matrix this is Lecture 16

Adjacency Matrix For a directed graph such as 1 2 3 4 5 6 1 2 3 4 5 6 The adjacency matrix is Lecture 16

Adjacency List And the adjacency list is The adjacency list representation is usually preferred, because it provides a compact way to represent sparse graphs A sparse graph is defined as one for which |E| is much less that |V| squared Sparse graphs are very common in real-world situations Lecture 16

Graphs in the Real World Take the snippet of a map shown We can easily represent this in diagrammatic form Suppose we know the distances between the towns Lecture 16

Graph Assuming A = Lemybrien, B = Kilmacthomas, C = Newtown D = Kilmeadan, E = Dunhill, F = Tramore G = Butlerstown, and H = Waterford 6 3 1 C D G 3 B 5 8 H 5 12 10 4 A E F Lecture 16

Representing Graphs An adjacency matrix representation may be preferred, however, when the graph is dense, or when we need to be able to tell quickly if there is an edge connecting two given vertices If a graph is a weighted graph, then the weight can be stored with the vertex in the adjacency list In Java we can do this using a class Node To store the weight in an adjacency matrix, simply store the weight instead of a 1 in the entry for vertex u and vertex v Lecture 16

Searching a Graph Breadth-First-Search (BFS) is a search algorithm which forms the basis for many important graph algorithms Given a graph G = (V, E) and a source vertex s, breadth-first search systematically explores the edges of G to discover every vertex that is reachable from s It also produces a “breadth-first-tree” with root s that contains all such reachable vertices For any vertex v, reachable from s, the path in the breadth-first tree from s to v corresponds to a “shortest path” from s to v in G. That is, a path containing the fewest number of edges The algorithm works on both directed and undirected graphs Lecture 16

Breadth First Search The algorithm is called Breadth-First-Search because it discovers all vertices at distance k from s before discovering any vertices at distance k+1 Using the BFS we speak of colouring the nodes as they are discovered. A white node has not yet been discovered. A grey node has been discovered but may have some adjacent white vertices. All adjacent vertices of a black node have been discovered The algorithm we are going to look at assumes the graph is represented as an adjacency list The algorithm also uses a queue to manage the set of grey vertices Lecture 16

BFS Algorithm The predecessor of u is stored in p[u], the distance from the source s to vertex u is stored in the array d[u] BFS(G, s) for each vertex u  V(G) - {s} colour[u] = white, d[u] =  endfor colour[s] = grey, d[s] = 0, Q = {s} while Q <> 0 u = head[Q] for each v  Adj[u] if colour[v] = white then colour[v] = grey, d[v] = d[u] + 1, Enqueue(Q, v) endif Dequeue(Q) colour[u] = black endwhile endalg Lecture 16

BFS Example    Q s (0)   Q w(1) r(1)         Q r(1) t(2) u r s t u    Q s (0) 1   Q w(1) r(1)      1   v w x y v w x y r s t u r s t u 1 2  Q r(1) t(2) x(2) 1 2  Q t(2) x(2) v(2)  1 2  2 1 2  v w x y v w x y r s t u r s t u 1 2 3 Q x(2) v(2) u(3) 1 2 3 Q v(2) u(3) y(3) 2 1 2  2 1 2 3 v w x y v w x y r s t u r s t u 1 2 3 Q u(3) y(3) 1 2 3 Q y(3) 2 1 2 3 2 1 2 3 v w x y v w x y r s t u 1 2 3 Q  2 1 2 3 v w x y

Analysis of BFS on G = (V,E) After initialisation, no vertex is ever whitened, so each vertex is enqueued only once, and hence dequeued only once. The operations of enqueueing and dequeueing take O(1) time, so the total time devoted to queue operations is O(V) The adjacency list of each vertex is scanned only when the vertex is dequeued, the adjacency list of each vertex is scanned at most once. Since the sum of the lengths of all the adjacency lists is E, at most O(E) time is spent in scanning the adjacency lists. The total running time of BFS is O(V + E) Breadth First Search runs in time linear in the size of the adjacency list G Lecture 16

Depth First Search DFS also runs in O(V+E) time. Instead of using a queue, however, the DFS can use a stack or can be implemented recursively DFS(G, s) for each vertex u  V[G] - {s} colour(u) = white, d[u] =  endfor S = {s}, d[s] = 0 while S <> 0 v = pop(S), colour(v) = black for each u  adj[v] if colour(u) = white then colour(u) = grey, d[u] = d[v] + 1, push(S, u) endif endwhile endalg Lecture 16

DFS Example    S s (0)   S r(1) w(1)          S v(2) t u r s t u    S s (0) 1   S r(1) w(1)      1   v w x y v w x y r s t u r s t u 1   S v(2) w(1) 1   S w(1) 2 1   2 1   v w x y v w x y r s t u r s t u 1 2  S t(1) x(2) 1 2 3 S u(3) x(2) 2 1 2  2 1 2  v w x y v w x y r s t u r s t u 1 2 3 S y(4) x(3) 1 2 3 S x(3) 2 1 2 4 2 1 2 4 v w x y v w x y r s t u 1 2 3 S  2 1 2 4 v w x y

Summary Graphs can be directed, or undirected, weighted or unweighted G = (V, E) can be represented As a collection of adjacency lists As an adjacency matrix Two methods of searching Breadth First Search Depth First Search The analogy used that BFS is like an army of searchers fanning out DFS is like a single searcher probing as deeply as possible, retreating only when hitting dead ends. BFS finds shortest paths, DFS doesn’t Lecture 16