GRAPHS Representation: Introduction
GRAPHS Representation: Introduction
Graph consists of a non empty set of points called vertices and a set of edges that link vertices. Formal Definition :
a set V={v1 , v2 .....,vn } of n >1 vertices and a set of E={e1 ,e2 ,.....,em } of m>0 edges such that each edge ek is corresponds to an un ordered pair of vertices (vi ,vj) where 0 < i,j <= n and 0 < k <= m.
A road network is a simple example of a graph, in which vertices reprents cities and road connecting them are correspond to edges.
V= { Delhi, Chenai, Kolkata, Mumbai, Nagpur } E= { (Delhi, Kolkata), (Delhi, Mumbai}, (Delhi, Nagpur), (Chenai, Kolkata), (Chenai, Mumbai), (Chenai, Nagpur), (Kolkata, Nagpur), (Mumbai, Nagpur) }
Start
Next
GRAPHS Representation :
Graphs can be represented by a diagram. For example, the graph representation of the above road network is depicted in the figure below.
Prev
Next
Print this page
Loop is an edge that connects a vertex to itself. Edge e6 in the figure below is a loop. Edges with same end vertices are called parallel edges . Edges e4 and e5 are parallel edges in the below figure.
Prev
Next
A Graph without loops and parallel edges is called a simple graph. A graphs with isolated vertices (no edges) is called null graph. Set of edges E can be empty for a graph but not set of vertices V. Graphs can be used in wide ranges of applications. For example consider the K nigsberg Bridge Problem. K nigsberg Bridge Problem: Two river islands B and C are formed by the Pregel river in K nigsberg (then the capital of East Prussia, Now renamed Kaliningrad and in west Soviet Russia) were connected by seven bridges as shown in the figure below. Start from any land areas walk over each bridge exactly once and return to the starting point.
Prev
Next
GRAPHS Representation :
Euler (1707-1783) in 1736 formulated the k nigsberg bridge problem as a graph problem and solved. Represent land area by vertices and bridges connecting them by edges. We get the following graph.
The problem is nothing but a children's game of drawing a figure without lifting pen from the paper and with out retracing a line.
Prev
Incidence: if an vertex vi is an end vertex of an edge ek , we say vertex vi is incident on ek and ek is incident on vi.
e1 is incident on v1 and v3 in the below figure. v4 is incidnet on e3 , e4 , and e5 in the figure below.
Degree: Degree of an vertex is number of edges incident on it, with loops counted twice.
Prev
Incidence: if an vertex vi is an end vertex of an edge ek , we say vertex vi is incident on ek and ek is incident on vi.
e1 is incident on v1 and v3 in the below figure. v4 is incidnet on e3 , e4 , and e5 in the figure below.
Degree: Degree of an vertex is number of edges incident on it, with loops counted twice.
Prev
Adjacent Edges: Two non parallel edges are adjacent if they have a vertex in common. e1 and e2 , e2 and e6 , e2 and e3 , e1 and e4 are adjacent edges in the above diagram.
Adjacent vertices: Two vertices are adjacent if they are connected by an edge. v1 and v3 , v1 and v2 , v2 and v4 are adjacent vertices in the above diagram.
Prev
Next
Graph Representation: There are several different ways to represent graphs in a computer. Two main representaions are Adjacency Matrix and Adjacency list.
Prev
GRAPHS Representation :
Accessing any element can be done quickly, in constant time. Size of the adjacency matrix is nXn for a graph of n vertices, which is independent of number of edges. The following representation uses memory efficiently for a sparse graph.
Prev
Next
Print this page
GRAPHS Representation :
Adjacency List Representation: It consists of a list of vertices, which can be represented either by linked list or array. For each vertex, adjacent vertices are represented in the form of a linked list. An example is given below.
Prev
Next
GRAPHS Representation :
Size of the adjacency lists is proposnal to number of edges and vertices. It is an efficient memory use for sparse graphs. Accessing an element takes linear time, since it involves traversal of linked list.
Prev
GRAPHS Representation :
Graph Traversals
Many graph problems can be solved by visiting each and every vertex in a systamatic manner. There are two main method to traversal graphs, Deapth First Search (DFS) and Breadth First Search (BFS).
Deapth First Search (DFS): Initially all vertices of the graph are unvisited. Start visiting the graph from any vertex, say v . For each unvisited adjacent vertex of v, search recurrsively. This process stops when all vertices reachable from v are visited. If there are any more unvisited vertices are present select any unvisted vertex and repeat the same search process. This method is called deapth first search since searching is done forward (deeper) from current node. The distance from start vertex is called deapth
GRAPHS Traversal :
Algorithm DFS ( G ) for i = 1 to n do // Initialize all vertices are unvisited status[i] = unvisited parent[i] = NULL
for i = 1 to n do if (status[i] == unvisited) // If there exits an invisited vertex, start traversal DF-Travel(i) Algorithm DF-Travel ( v ) status[v] = visited for each vertex u adjacent to v do if status[u] == unvisited then parent[u] = v DF-Travel ( u )
Prev
GRAPHS Traversal :
Breadth First Search (BSF): Initially all vertices of the graph are unvisited. Start visiting the graph from any vertex, say v . Visit each unvisited adjacent vertex of v. Repeat the process for each vertex visited. This process stops when all vertices reachable from v are visited. If there is any more unvisited vertices are present select any unvisted vertex and repeat the same search process.
This method is called breadth first search, since it works outward from a center point, much like the ripples created when throwing a stone into a pond. It moves outward in all directions, one level at a time. BSF can be implemented using queue to maintain set vertices visited first time and their adjacent vertices are not explored for them. Algorithm BFS ( G )
Algorithm DF-Travel ( v ) status[v] = visited Q = {v} while ( Q is not empty ) do { u = delete-queue(Q) for each vertex w adjacent to u do if status[w] == unvisited then { status[w] = visited insert-queue(Q, w) parent[w] = u } }
for i = 1 to n do // Initialize all vertices are unvisited status[i] = unvisited parent[i] = NULL for i = 1 to n do
Next
2. Implement the DFS/BFS algorithm. 3. Design an algorithm to find given graph is cyclic or not.
Prev
End