ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa.

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.
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 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.
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.
David Luebke 1 5/9/2015 CS 332: Algorithms Graph Algorithms.
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?
Zhengjin Graphs: Adjacency Matrix ● Example: a d bc A ?? 4.
Breadth First Search. Two standard ways to represent a graph –Adjacency lists, –Adjacency Matrix Applicable to directed and undirected graphs. Adjacency.
Graph Searching (Graph Traversal) Algorithm Design and Analysis Week 8 Bibliography: [CLRS] – chap 22.2 –
1 Graph Programming Gordon College. 2 Graph Basics A graph G = (V, E) –V = set of vertices, E = set of edges –Dense graph: |E|  |V| 2 ; Sparse graph:
Lecture 19: Shortest Paths Shang-Hua Teng. Weighted Directed Graphs Weight on edges for distance
1 Data Structures DFS, Topological Sort Dana Shapira.
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.
CISC220 Fall 2009 James Atlas Nov 13: Graphs, Line Intersections.
1 7/2/2015 ITCS 6114 Graph Algorithms. 2 7/2/2015 Graphs ● A graph G = (V, E) ■ V = set of vertices ■ E = set of edges = subset of V  V ■ Thus |E| =
Tirgul 7 Review of graphs Graph algorithms: – BFS (next tirgul) – DFS – Properties of DFS – Topological sort.
David Luebke 1 8/7/2015 CS 332: Algorithms Graph Algorithms.
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.
Graphs – Shortest Path (Weighted Graph) ORD DFW SFO LAX
COSC 3101A - Design and Analysis of Algorithms 10
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.
CSE 2331 / 5331 Topic 12: Shortest Path Basics Dijkstra Algorithm Relaxation Bellman-Ford Alg.
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.
Introduction to Graphs And Breadth First Search. Graphs: what are they? Representations of pairwise relationships Collections of objects under some specified.
CSC 413/513: Intro to Algorithms Graph Algorithms.
Graph Algorithms Searching. Review: Graphs ● A graph G = (V, E) ■ V = set of vertices, E = set of edges ■ Dense graph: |E|  |V| 2 ; Sparse graph: |E|
Chapter 22: Elementary Graph Algorithms
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 20.
David Luebke 1 1/6/2016 CS 332: Algorithms Graph Algorithms.
CS 2133: Algorithms Intro to Graph Algorithms (Slides created by David Luebke)
David Luebke 1 1/25/2016 CSE 207: Algorithms Graph Algorithms.
Trees Thm 2.1. (Cayley 1889) There are nn-2 different labeled trees
COSC 3101A - Design and Analysis of Algorithms 9 Knapsack Problem Huffman Codes Introduction to Graphs Many of these slides are taken from Monica Nicolescu,
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.
CSE Graph Search Algorithms. CSE Graph a c b Node ~ city or computer Edge ~ road or data cable Undirected or Directed A surprisingly large.
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture nine Dr. Hamdy M. Mousa.
Chapter 05 Introduction to Graph And Search Algorithms.
G RAPH A LGORITHMS Dr. Tanzima Hashem Assistant Professor CSE, BUET.
Lecture 20. Graphs and network models 1. Recap Binary search tree is a special binary tree which is designed to make the search of elements or keys in.
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.
Introduction to Algorithms
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
Graph: representation and traversal CISC4080, Computer Algorithms
Elementary Graph Algorithms
Graph Representation Adjacency list representation of G = (V, E)
Lecture 10 Algorithm Analysis
Finding Shortest Paths
Advanced Algorithms Analysis and Design
Chapter 22: Elementary Graph Algorithms I
Basic Graph Algorithms
Elementary Graph Algorithms
CSC 325: Algorithms Graph Algorithms David Luebke /24/2019.
Presentation transcript:

ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture eight Dr. Hamdy M. Mousa

Graph Algorithms

This chapter presents methods for representing a graph and for searching a graph. Searching a graph means systematically following the edges of the graph so as to visit the vertices of the graph. Given graph G = (V, E), May be either directed or undirected. we usually measure the size of the input in terms of the number of vertices |V| and the number of edges |E| of the graph. The above graph G having five vertices and seven edges. Elementary Graph Algorithms

Representations of graphs The two most common computational representations of graphs: –adjacency lists - adjacency matrices When expressing the running time of an algorithm, it’s often in terms of both|V| and |E|. In asymptotic notation, and only in asymptotic notation, Example: O(V + E).

adjacency-list representation The adjacency-list representation of a graph G = (V, E) consists of an array Adj of |V| lists, one for each vertex in V. For each u  V, the adjacency list Adj[u] contains all the vertices v such that there is an edge (u, v)  E. That is, Adj[u] consists of all the vertices adjacent to u in G.

Adjacency lists Array Adj of |V| lists, one per vertex. Vertex u’s list has all vertices v such that (u, v)  E. (Works for both directed and undirected graphs.) Example: For an undirected graph:

Adjacency lists If G is a directed graph, the sum of the lengths of all the adjacency lists is |E|, since an edge of the form (u, v) is represented by having v appear in Adj[u]. If G is an undirected graph, the sum of the lengths of all the adjacency lists is 2 |E|, since if (u, v) is an undirected edge, then u appears in v’s adjacency list and vice versa. For both directed and undirected graphs, the adjacency- list representation has the desirable property that the amount of memory it requires is  (V + E). If edges have weights, can put the weights in the lists. Weight: w : E  R

Adjacency lists We’ll use weights later on for spanning trees and shortest paths. Space:  (V + E). Time: to list all vertices adjacent to u:  (degree(u)). Time: to determine if (u, v)  E: O(degree(u)). Example: For a directed graph: A potential disadvantage of the adjacency-list representation is that there is no quicker way to determine if a given edge (u, v) is present in the graph than to search for v in the adjacency list Adj[u].

Adjacency matrix Graph G = (V, E), assume that the vertices are numbered 1, 2,..., |V| The adjacency-matrix representation of a graph G consists of a |V| × |V| matrix A = (ai j ) such that Space:  (V 2 ). Time: to list all vertices adjacent to u:  (V). Time: to determine if (u, v)  E:  (1). Can store weights instead of bits for weighted graph.

It is one of the simplest algorithms for searching a graph and the archetype for many important graph algorithms. Given a graph G = (V, E) and a distinguished source vertex s, Breadth-first search systematically explores the edges of G to “discover” every vertex that is reachable from s. It computes the distance (smallest number of edges) from s to each reachable vertex. It also produces a “breadth-first tree” with root s that contains all reachable vertices. The algorithm works on both directed and undirected graphs. Breadth-first search

To keep track of progress, breadth-first search colors each vertex white, gray, or black. If (u, v)  E and vertex u is black, then vertex v is either gray or black; that is, all vertices adjacent to black vertices have been discovered. initially containing only its root, which is the source vertex s. white vertex v is discovered vertex u, in the adjacency list the vertex v and the edge (u, v) are added to the tree. We say that u is the predecessor or parent of v in the breadth-first tree. Since a vertex is discovered at most once, it has at if u is on a path in the tree from the root s to vertex v, then u is an ancestor of v and v is a descendant of u. Breadth-first search

the input graph G = (V, E) adjacency list. color[u] is The color of each vertex u  V π[u] is the predecessor of u If u has no predecessor, then π[u] = NIL. d[u] is The distance from the source s to vertex u Uses a first-in, first-out queue Q to manage the set of gray vertices. breadth-first-search procedure (BFS)

Lines 1–4 paint every vertex white, set d[u] to be  for each vertex u, and set the parent of every vertex to be NIL. Line 5 paints the source vertex s gray line 7 sets the predecessor of the source to be NIL. Lines 8–9 initialize Q to the queue containing just the vertex s. At the test in line 10, the queue Q consists of the set of gray vertices.

The operation of BFS on an undirected graph

Shortest paths The breadth-first search finds the distance to each reachable vertex in a graph G = (V, E) from a given source vertex s  V. Define the shortest-path distance δ(s, v) from s to v as the minimum number of edges in any path from vertex s to vertex v; if there is no path from s to v, then δ(s, v)=∞.

Lemma 22.1 Let G = (V, E) be a directed or undirected graph, and let s  V be an arbitrary vertex. Then, for any edge (u, v)  E, δ(s, v) ≤ δ(s, u) + 1. Proof If u is reachable from s, then so is v. In this case, the shortest path from s to v cannot be longer than the shortest path from s to u followed by the edge (u, v), and thus the inequality holds. If u is not reachable from s, then δ(s, u) = ∞, And the inequality holds. We want to show that BFS properly computes d[v] = δ(s, v) for each vertex v  V. We first show that d[v] bounds δ(s, v) from above.