Download presentation

Presentation is loading. Please wait.

Published byLorena Jefferson Modified over 3 years ago

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

2
Graph Algorithms

3
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

4
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).

5
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.

6
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:

7
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

8
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].

9
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.

10
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

11
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

12
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)

13
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.

14
The operation of BFS on an undirected graph

15
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)=∞.

16
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.

Similar presentations

OK

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.

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.

© 2018 SlidePlayer.com Inc.

All rights reserved.

To ensure the functioning of the site, we use **cookies**. We share information about your activities on the site with our partners and Google partners: social networks and companies engaged in advertising and web analytics. For more information, see the Privacy Policy and Google Privacy & Terms.
Your consent to our cookies if you continue to use this website.

Ads by Google

Ppt on uses of water Ppt on power generation using speed breaker Ppt on photo editing Ppt on metro rail in hyderabad india Ppt on recycling of waste Ppt on high voltage dc transmission Ppt on travel and tourism in india free download Ppt on cross-sectional study Ppt on polynomials download Ppt on fdi in indian economy