elementry graph algorithm

Upload: neha-verma

Post on 05-Nov-2015

230 views

Category:

Documents


1 download

DESCRIPTION

c

TRANSCRIPT

Definition

ELEMENTARY GRAPH ALGORITHMS

INTRODUCTION

Graphs can be used in the following area like Computer networks, Representation of topological information (maps), precedence graphs, workflows, Semantic networks (e.g., entity-relationship diagrams), Finding Minimum spanning tree, Finding the shortest path etc.

DEFINITION A graph is a pair G = (V, E) where V is a set of vertices and E is a binary relation on V. E contains a pair (u, v) if there is an edge between the u and v vertices. If the graph is directed, this pair is ordered: (u, v) is not the same as (v, u).

Example: Graphs

GRAPHS -TERMINOLOGY:Directed and undirected graph: A directed graph (or: digraph) G is a pair (V, E) where V is a set of vertices and E is a set of ordered pairs of vertices, called(directed) edges, if E is a set of unordered pairs, G is called undirected graph.

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.

Simple Graphs: Graphs in which there is no more than one edge between any two vertices

Degree of a vertex: the number of edges joined to that vertex. If a vertex has no edges, its degree is zero.

Path: A path is a sequence of vertices traversed by following the edges between them.Cycle: A cycle is a path that includes the same vertex two or more times. A graph without cycles is acyclic. Directed Acyclic Graphs are called dags.

Sparse graph: A Sparse graph is a graph in which the numbers of edges are much less than the maximal number of edges i.e. |E| is much less than |V|2.Dense graph: A dense graph is a graph in which the numbers of edges are close to the maximal number of edges i.e. |E| is close to |V|2Complete graph: a complete graph is a simple graph in which every pair of distinct vertices is connected by a unique edge.Connected, strongly connected: An undirected graph is connected if and only if, for each pair of vertices u and v, there is a path from u to v. But a directed graph is called strongly connected if and only if for each pair of vertices u and v, there is a path from u to v.

Here the connected word is used for undirected graph but the word strongly connected is used for directed graph because in an undirected graph, if there is a path from u to v, then there is automatically a path from v to u. in a directed graph, this may not be true, so the qualifier Strongly is used to indicate that condition is stronger. Connected, strongly connected component: A connected component of an undirected graph G is a maximal connected subgraph of G.

Example: A graph with three connected component

Example: A graph with strongly connected component

GRAPH REPRESENTATIONS:There are two common ways to represent a graph G = (V, E) as:

Adjacency lists

Adjacency matrixThese common ways are applicable to represent both directed and undirected graphs. The adjacency-list representation is usually preferred, when the graph is sparse because it provides a compact way to represent sparse graphs. An adjacency-matrix representation may be preferred when the graph is dense.Adjacency list:

The adjacency-list representation of a graph consists of an array Adj of |V| lists, one for each vertex in V. For each u V, The adjacency list Adj[u] consists of all the vertices adjacent to u in G. The vertices in each adjacency list are typically stored in an arbitrary order.

An array indexed by vertex number containing linked lists (adjacency lists)

The ith array entry contains all edges of G that leave vi. Edges that are not in G do not require any storage

If G is a directed graph, the sum of the lengths of all the adjacency lists is |E|. If G is an undirected graph, the sum of the lengths of all the adjacency lists is 2|E|. Adjacency lists works for weighted graphs i.e. if edges have weights, we can put the weights in the lists.

Example: Adjacency-list representation of undirected graph

Example: Adjacency-list representation of directed graph

Analysis:

The amount of memory it requires is: (V + E).

It requires time to list all vertices adjacent to u is: O (degree (u)).

It requires time to determine if (u, v) , E is: O (degree (u)).

Adjacency-matrix: The adjacency-matrix representation of a graph G = (V, E), consists of a |V| |V| matrix, matrix A = (aij) such that

Example: Adjacency-matrix representation of undirected graph

Example: Adjacency-matrix representation of undirected graph

Analysis:

Good for dense graphs

The amount of memory it requires is: (V2).

It requires time to list all vertices adjacent to u is: (V).

It requires time to determine if (u, v) E is: (1).

Adjacency list vs. adjacency matrix:

Usually, adjacency list is better and consumes much less memory.

TRAVERSING GRAPHSMany algorithms on graphs examine each vertex (and edge), Two common traversal strategies that visit each vertex (or edge) exactly once are as follows:

Depth-First Search Breadth-First Search

Many advanced graph algorithms based on these searching strategies are as follows: Finding (strongly) connected components

Topological sorting

Breadth-first search

Breadth-first search is one of the easy and simplest algorithms for searching a graph and the algorithm works on both directed and undirected graphs. The algorithms like Prims minimum-spanning-tree algorithm and Dijkstra's single-source shortest-paths algorithm use similar idea to those in breadth-first search. The Algorithm for BFS is given below:

How BFS work:The procedure of BFS can be summarized as follows: For a given graph G, breadth-first search starts at some source vertex s and discovers which vertices are reachable from s. Breadth-first search discovers vertices in increasing order of distance, so it can be used as an algorithm for finding the shortest paths.

At any given time there is a frontier of vertices that have been discovered, but not yet processed. Breadth-first search is named because it visits vertices across the entire breadth of this frontier.

Initially all vertices except the source are colored white, which means that they are undiscovered.

When a vertex has first been discovered, it is colored gray and is part of the frontier. When a gray vertex is processed, then it becomes black.

The breath- first search use a queue (first-in first-out list), where elements are removed in the same order they are inserted.

We will also maintain arrays color[u] which holds the color of vertex u, either white, gray or black, [u] which points to the predecessor of u i.e. the vertex that first discovered u, and d[u], the distance from s to u.

Analysis: For loop in line first for initialization requires (V) time. The while loop runs at most V number of times because we never visit a vertex twice. The number of iterations of the inner for loop is proportional to deg (u) + 1. The +1 is because even if deg (u) = 0, we need to spend a constant amount of time to set up the loop. Therefore the total running can be evaluated as:

Example: Illustrates the process of BFS on the below sample graph.

Example: Consider the graph G shown in below figure, Illustrates the process of BFS

Depth-first search (DFS)In the breadth-first search, the predecessor subgraph forms a tree whereas the predecessor subgraph produced by a depth-first search may be composed of several trees, because the search may be repeated from multiple sources. The predecessor subgraph of a depth-first search forms a depth-first forest composed of several depth-first trees.

DFS uses the backtracking technique, the Depth first search (DFS) is useful for

Find a path from one vertex to another

Whether or not graph is connected

Computing a spanning tree of a connected graph.

How DFS work:The procedure of DFS can be summarized as follows:

For DFS the input graph G = (V, E), may be directed or undirected and there is no source vertex is given.

We maintain a color for each vertex as: WHITE means undiscovered GRAY means discovered but not finished processing BLACK means finished We also store predecessor pointers, pointing back to the vertex that discovered a given vertex. We will also associate two numbers with each vertex. These are time stamps.

d [v] = discovery time

f [v] = finishing time

When we first discover a vertex v store a counter in d[v] and when we are finished processing a vertex we store a counter in f[v].AnalysisIf we ignore the time spent in the recursive calls of DFS, then we can say that each vertex u can be processed in O (1+outdeg (u)) time. Thus the total time can be evaluated as

Example: Illustrates the process of DFS on the below sample graph.

Solution:

Example: Illustrates the process of DFS on the below sample graph.

Solution:

Classification of edges:

The edge classification can be used to glean important information about a graph. For example, a directed graph is acyclic if and only if a depth-first search yields no "back" edges.Classification of edges by type:

Tree edge: in the depth-first forest. Found by exploring (u, v).

Back edge: (u, v), where u is a descendant of v (in the depth-first tree). Self loops, which may occur in directed graphs, are considered to be back edges. Forward edge: (u, v), where v is a descendant of u, but not a tree edge.

Cross edge: any other edge. Can go between vertices in same depth-first tree or in different depth-first trees

Classification of edges by color:

Each edge (u, v) can be classified by the color of the vertex v that is reached when the edge is first explored:

White indicates a tree edge

Gray indicates a back edge

Black indicates a forward or cross edge

Parenthesis theorem:

In any depth-first search of a graph G = (V, E), for any two vertices u and v, exactly one of the following three conditions holds:

If we represent the discovery of vertex v with a left parenthesis "(v" and represent its finishing by a right parenthesis "v)", then the history of discoveries and finishing makes a well-formed expression in the sense that the parentheses are properly nested.

Example: show the parenthesis structure of the following given graph

Solution:

Let a be the start vertex, after applying the depth first search on the given graph, we have

TOPOLOGICAL SORT Depth-first search can be used to perform a topological sort of a directed acyclic graph. A topological sort of a dag G = (V, E) is a linear ordering of vertices such that if G contains an edge (u, v), then u appears before v in the ordering. The linear ordering of vertices is not possible if the graph is not acyclic. A topological sort of a graph can be viewed as an ordering of vertices along a horizontal line so that all directed edges go from left to right. Topological sorting is different from the usual kind of "sorting". To compute a topological ordering is very easy, for every edge (u, v) in a DAG, the finish time of u is greater than the finish time of v. Thus, it suffices to output the vertices in reverse order of finishing time.

Analysis:

The topological sort run in (V + E) time, since depth-first search takes (V + E) time and it takes O (1) time to insert each of the |V| vertices onto the front of the linked list.

Example: Illustrate the process of TOPOLOGICAL-SORT on the following given graph.

Solution: We can use depth-first search to perform a topological sort on a directed acyclic graph.

STRONGLY CONNECTED COMPONENTSDecomposing a directed graph into its strongly connected components is a classic application of DFS. Two vertices of directed graph are in the same component if and only if they are reachable from each other. A strongly connected component of a directed graph G is a maximal set of vertices

such that for every pair of vertices u and v, there is a directed path from u to v and a directed path from v to u.

For example, consider the following directed graph

The above directed graph has four strongly connected components,

Example: Illustrate how the procedure STRONGLY-CONNECTED-COMPONENTS works on the below figure.

Solution:

Step 1: Call DFS (G) to compute finishing times f[u] for each vertex u

Step 2: compute GT

Step 3: call DFS (GT)

Example: Find the STRONGLY-CONNECTED-COMPONENTS in the following given graph.

Solution:

PAGE 30