Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.

Slides:



Advertisements
Similar presentations
What is a graph ? G=(V,E) V = a set of vertices E = a set of edges edge = unordered pair of vertices
Advertisements

Introduction to Algorithms Lecture 12 Prof. Constantinos Daskalakis CLRS
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.
Tirgul 7 Review of graphs Graph algorithms: –DFS –Properties of DFS –Topological sort.
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.
Introduction to Algorithms Second Edition by Cormen, Leiserson, Rivest & Stein Chapter 22.
CS 473Lecture 141 CS473-Algorithms I Lecture 15 Graph Searching: Depth-First Search and Topological Sort.
CS 473Lecture 141 CS473-Algorithms I Lecture 14-A Graph Searching: Breadth-First Search.
Graph Traversals. For solving most problems on graphs –Need to systematically visit all the vertices and edges of a graph Two major traversals –Breadth-First.
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.
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?
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.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
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 Graph Algorithms Andreas Klappenecker [based on slides by Prof. Welch]
Graph traversals / cutler1 Graph traversals Breadth first search Depth first search.
Tirgul 8 Graph algorithms: Strongly connected components.
Tirgul 11 DFS Properties of DFS Topological sort.
CPSC 311, Fall CPSC 311 Analysis of Algorithms Graph Algorithms Prof. Jennifer Welch Fall 2009.
CS 311 Graph Algorithms. Definitions A Graph G = (V, E) where V is a set of vertices and E is a set of edges, An edge is a pair (u,v) where u,v  V. If.
CPSC 411 Design and Analysis of Algorithms Set 8: Graph Algorithms Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 8 1.
1 Data Structures DFS, Topological Sort Dana Shapira.
Tirgul 11 BFS,DFS review Properties Use. Breadth-First-Search(BFS) The BFS algorithm executes a breadth search over the graph. The search starts at a.
CSE 780 Algorithms Advanced Algorithms Graph Algorithms Representations BFS.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 12 Graphs and basic search algorithms Motivation Definitions and properties Representation.
Lecture 10 Graph Algorithms. Definitions Graph is a set of vertices V, with edges connecting some of the vertices (edge set E). An edge can connect two.
Graph Algorithms Introduction to Algorithms Graph Algorithms CSE 680 Prof. Roger Crawfis Partially from io.uwinnipeg.ca/~ychen2.
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.
November 6, Algorithms and Data Structures Lecture XI Simonas Šaltenis Aalborg University
COSC 3101A - Design and Analysis of Algorithms 10
Elementary Graph Algorithms CSc 4520/6520 Fall 2013 Slides adapted from David Luebke, University of Virginia and David Plaisted, University of North Carolina.
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.
Elementary Graph Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
 2004 SDU Lectrue4-Properties of DFS Properties of DFS Classification of edges Topological sort.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
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.
Chapter 05 Introduction to Graph And Search Algorithms.
November 19, Algorithms and Data Structures Lecture XI Simonas Šaltenis Nykredit Center for Database Research Aalborg University
CSE 421 Algorithms Richard Anderson Autumn 2015 Lecture 5.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
CSC317 1 At the same time: Breadth-first search tree: If node v is discovered after u then edge uv is added to the tree. We say that u is a predecessor.
Chapter 22: Elementary Graph Algorithms Overview: Definition of a graph Representation of graphs adjacency list matrix Elementary search algorithms breadth-first.
Nattee Niparnan. Graph  A pair G = (V,E)  V = set of vertices (node)  E = set of edges (pairs of vertices)  V = (1,2,3,4,5,6,7)  E = ((1,2),(2,3),(3,5),(1,4),(4,
CS138A Elementary Graph Algorithms Peter Schröder.
Introduction to Algorithms
Graphs – Breadth First Search
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Chapter 22 Elementary Graph Algorithms
CSC317 Graph algorithms Why bother?
Graphs Breadth First Search & Depth First Search
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Basic Graph Algorithms
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort

Graph – a definition: A directed graph, G, is a couple (V,E) such that V is a finite set and E is a subset of V  V. The set V is denoted as the vertex set of G and the set E is denoted as the edge set of G. Note that a directed graph may contain self loops (an edge from a vertex to itself). In an undirected graph, the edges in E are not ordered, in the sense of that an edge is a set {u,v} instead of an ordered couple (u,v).

Some important graph definitions: Sub-graph: Let G(V,E) be a graph. We say that G’(E’,V’) is a sub-graph of G if V’  V and E’  E  V’  V’ Path: Let u,v be vertices in the graph. A path of length k between u and v is a sequence of vertices, v 0,…,v k, such that v 0 =v, v k =u, and for each i  {0..k-1}, (v i, v i+1 )  E. We say that v i is the predecessor v i+1 on the path If there is a path from v to u we say that v is an ancestor of u and u is a descendant of v. Cycle: In a directed graph, a cycle is a path v 0,..,v k, such that v 0 =v k. If the vertices v 1,…,v k are also pair wise disjoint, the cycle is called simple. In an undirected graph, a (simple) cycle is a path v 0,…,v k such that v 0 =v k, k  3 and v 1,…,v k are pair wise disjoint.

more important definitions… Connected graph: An undirected graph G is said to be connected if for each two vertices u,v in the graph, there is a path between u and v. Strongly Connected graph: A directed graph G is said to be strongly connected if for each two vertices u,v in the graph, there is a path between u and v. Tree: A tree is an undirected, connected, a-cyclic graph. Rooted Tree: A directed graph G is called a rooted tree if there exists s  V s.t. for each v  V, there is exactly one path between s and v. Forest: A forest (rooted forest) is a set of disjoint trees (rooted trees).

Graph representations: adjacency lists One natural way to represent graphs is to use adjacency lists. For each vertex v there is a linked list of his neighbors. This representation is good for sparse graphs, since we use only |V| lists and in a sparse graph, each list is short (overall representation size is V+E).

Graph representations: adjacency matrix Another way to represent a graph in the computer is to use an adjacency matrix. This is a matrix of size |V|  |V|, we will denote it by T. The vertices are enumerated, v 1,…,v |V|. Now, T i,j =1  there is an edge between the vertices v i and v j  (v i,v j )  E. If the graph is undirected: T i,j =1  T j,i =1 * what is the meaning of T 2, T 3, etc. ???

Review of graphs Graphs are a very useful tool in Computer Science. Many problems can be reduced to problems on graphs, and there exists many efficient algorithms that solves graph problems. Today we will examine a few of these algorithms. We will focus on the shortest path problem (unweighted graphs) which is a basic routine in many graph related algorithm. We can define: –Shortest path between s and t. –Single source shortest path (shortest path between s and {V}). –All pairs shortest path.

Breadth First Search (BFS) The Breadth First Search (BFS) is one of the simplest and most useful graph algorithms. The algorithm systematically explores the edges of G to find all vertices that are reachable from s and computes distances to those vertices. It also produces a “breadth first tree”, with s being the root. It is called breadth first search since it expands the frontier between visited and non visited vertices uniformly across the breadth of the frontier.

Breadth First Search (cont.) To keep track of progress, BFS colors each vertex according to their status. Vertices are initialized in white and are later colored as they are discovered and being processed. It also produces a “breadth first tree”, with s being the root. If and u is black then v is non white. Gray vertices represent the frontier between discovered and undiscovered vertices.

Breadth First Search (cont.) The BFS algorithm constructs a BFS tree, initially containing only the root s (the source vertex). While scanning the neighbors of an already discovered vertex u, whenever a white vertex v is discovered it is added to the tree along with the edge (u,v). u is the parent of v in the BFS tree. If u is on the pass in the tree from s to v then u is ancestor of v and v is a descendant of u. The algorithm uses a queue (FIFO) to manage the set of gray vertices.

BFS – pseudo code BFS(G,s) //initializing. for each vertex u  V[G]\{s} { color[u] = white; dist[u] = ∞; parent[u] = NULL; } color[s] = GRAY; dist[s] = 0; parent[s] = NULL; Q <- {s};

BFS – pseudo code (cont.)... while (not Q.isEmpty()) { u <- Q.head(); foreach v  u.neighbors() { if color[v] ≠ WHITE { color[v] = GRAY; dist[v] = dist[u]+1; parent[v] = u; Q.enqueue(v); } Q.dequeue(); color[u] = BLACK; }

BFS, an example: ∞ r 0 s ∞ t ∞∞∞ uvw 1 r 0 s ∞ t ∞1∞ uvw 1 r 0 s 2 t ∞12 uvwuvw 1 r 0 s 2 t 212 Q s QQ Q w Q u vrrtw Q twu uvw 1 r 0 s 2 t 212 u uvw 1 r 0 s 2 t 212 uvw 1 r 0 s 2 t 212 Q Ø

BFS, properties: What can we say about time complexity? Why does it works? (intuition): –We can think as if we have a set of nodes S and for all the nodes in S, the distance is correct (S begins with just s). –At step t, S contains the t closest nodes to s. –At each step, the algorithm adds to S the next closest node to s by finding the closest node to s in S that has neighbors out of S and adding these neighbors to S (greedy algorithm). –The proof of correctness uses the fact that we have already discovered closer nodes and assigned them the correct distance when we discover a new node that is a neighbor of one of them.

BFS, proof of correctness: Claim 1: Let G=(V,E) be a graph and let s  V be an arbitrary vertex. Then for any edge (u,v)  E : Proof 1: If u is reachable from s, so is v, otherwise Claim 2: Let G=(V,E) be a graph, and suppose we run BFS on G from s. Upon termination, Proof 2: The proof is by induction on the number of times a vertex is placed in Q. The claim holds after placing s in Q (basis). For the induction step, let’s look at a white vertex v discovered during the search from u. By the hypothesis. From claim 1 and the algorithm we get:

BFS, proof of correctness (cont.) : Claim 3: Suppose that during the execution of BFS on graph G, the queue Q contains the nodes. Then: Proof 3: The proof is by induction on the number of queue operations. The basis holds (only s is in the queue). When dequeuing a vertex, and the claim holds. When enqueuing a node w, we have the node u at the head of the queue => and we also have:

BFS, proof of correctness (cont.) : Claim 4: Let G=(V,E) be a graph and we run BFS from s  V on G. Then the BFS discovers every vertex v  V that is reachable from s, and upon termination, Proof 4: If v is unreachable, we have, but since v hasn’t been discovered since it has been initialized, we get: For vertices that are reachable from s, we define For each v  V k we show by induction that during the execution of the BFS, there is at most one point at which: –v is grayed. –dist[v] is set to k. –if v≠s then parent[v] is set to u for some u  V k-1. –v is inserted into the queue Q.

BFS, proof of correctness (cont.) : Proof 4 (cont.): For k=0, the inductive hypothesis holds (basis). For the inductive step, we first note that Q is never empty during the algorithm execution and that once a vertex v is entered Q, dist[v] and parent[v] never changes. Let us consider an arbitrary vertex v  V k (k > 1). From claim 3 (monotonicity),claim 2 (dist[v] ≥k) and the inductive hypothesis we get that v must be discovered after all vertices in V k-1 are enqueued (if discovered at all). Since, there is a path of length k from s to v => There is a vertex u  V k-1 such that (u,v)  E. Let u be the first such vertex grayed. u will appear as the head of Q, at that time, its neighbors will be scanned and v will be discovered => d[v] = d[u]+1 = k and parent[v] = u.