You cannot copy content of this page.

Oct 13,2012
Leave a comment
By admin

**Representation**- Adjacency Matrix
- Adjacency Lists

**Traversal schemes**- DFS (Depth First Search)
- Breadth First Search (BFS)

**Minimal Spanning**- Kruskal
- Prim

**Graph: **A graph consists of a set of nodes (vertices) and a set of arcs (or edges).

- The set of nodes {1,2,3,4}
- The set of arcs or edges {(1,2), (1,3), (1,4), (2,4), (2,3), (3,4)}

Thus a graph G consists of two sets V and E. V is a finite non-empty set of vertices. E is a set of pairs of vertices. These pairs are called edges. V(G) and E(G) will represent the sets of vertices and edges of graph G.

Thus, we may write G=(V,E) to represent a graph.

Tree |
Graph |

Root | No root |

Parent-child relationship | No such relationship |

Connected | May be disconnected |

acyclic |
May contain cycle |

(do not contain any cycle) |

**Directed Graphs:** If the pairs of nodes/vertices that make up the arcs are ordered pairs (direction of movement is used) the graph is said to be a directed graph (or digraph).

**NOTE:**

- The arrows between nodes represent arcs. The head of each arrow represents the second ndoe in the ordered pair of nodes making up an arc, and the tail of each arrow represents the first node in the pair.
- We use parenthesis to indicate an unordered pair and angular brackets to indicate an ordered pair.
- A graph need not be a tree but a tree must be a graph.
- Nodes need not have any arc associated with them.

**Incident Node: **A node n is incident to an arc x, if n is one of the two nodes in the ordered pair of nodes that constitute x. (We may also say that x is incident to n).

**Degree of a node:** The degree of a node is the number of arcs incident to it.

**Indegree:** The indegree of a node n is the number of arcs that have n as the head. (Arrows coming in n).

**Example:**

**Outdegree:** The outdegree of n is the number of arcs that have n as the tail. In the above graph, outdegree of 3 is 0 as it has no other node, for which it is a tail.

Outdegree of 2 is 2 i.e. {<2,1>, <2,3>}

**Note: **Maximum number of edges in any n vertex undirected graph is n(n-1)/2

**Complete Graph:** A n vertex undirected graph with exactly n(n-1)/2 edges is said to be a complete graph.

**Adjacent node:** A node n is adjacent to a node m if there is an arc from m to n. If n is adjacent to m, n is called a successor of m and m is called a predecessor of n.

**Weighted Graph or Network:** When a number is associated with each arc/edge i.e. edges have properties or attributes the graph is called a weighted graph or a network.

**Adjacency Matrix****Adjacency Lists**

**Connected Graph: **A graph is said to be connected if every pair of vertices in a graph is connected.

**Component of a graph:** A component of a graph is a maximal connected subgraph. In the above example there are two components.

**(i) Adjacency Matrix Representation:** A graph g with n vertices can be represented by a n*n matrix.

Since the above graph has 7 vertices, hence it can be represented by 7 X 7 matrix.

**Computing Degree of a Vertex:**

**(i) For an undirected graph:-** Degree is equal to sum of corresponding row = sum of corresponding column. For example: In the above table Degree of V2 = sum of row corresponding to V2 = sum of column corresponding to V2. (as shown by dotted lines), which is 2.

**(ii) For a directed graph:- **

Number of vertices = 4

Order of Adjacency Matrix = R x C = 4 x 4

**Note:**

- Sum of 1’s of the row gives outdegree.
- Sum of 1’s of the columns gives indegree.
- Sum of indegree + outdegree gives degree of that node

**Example:** In the above figure, Outdegree of V1 is 3 and Indegree of V1 is 0. Thus, Degree of V1 is 3+0=3.

**Adjacency Matrix for the above digraph is given below:**

**Finding total number of nodes:**

**(i) For an undirected graph: **To find the total number of edges of the graph, we find the sum of all 1’s except diagonal and divide it by 2 and add all 1’s of the diagonal.

**Example:**

Total number of edges = Sum of all 1’s except diagonal

= 8/2 = 4 + all 1’s of the diagonal ([ ]), which are all zero.

Thus, total number of edges = 4

**(ii) For a directed graph:** For a directed graph, total number of edges are given by sum of all 1’s.

**Example:**

Total number of edges = 5

**Implementation of Adjacency Matrix:**

**Example 1: **

Program to implement the above graph is as follows:

**Adjacency List:** We have an array of n elements (for a graph of n vertices). Each element has a linked list associated with it, which corresponds to the vertices, which are adjacent to the vertex.

**NOTE:
**

- Adjacent of node V1 is V2 and V3.
- Adjacent of node V2 is V1 and V4.
- Adjacent of node V3 is V1 and V4.
- Adjacent of node V4 is V2 and V3.

**For Graph G1:**

- Total number of edges in the graph = (Total number of nodes)/2 = 8/2 = 4 edges.
- Degree of a vertex can be found by counting the number of nodes for that particular value.

- Degree of vertex V1 = 2
- Degree of vertex V2 = 2
- Degree of vertex V3 = 2
- Degree of vertex V4 = 2

**For a directed graph:**

- Total number of edges in graph is equal to total number of nodes.
- Degree of a node is equal to number of nodes for that vertex.
- Outdegree of a vertex can be found out by simply counting the nodes of that element.

The indegree is calculated by traversing all the nodes and counting the occurrences of that vertex.

**NOTE:
**

- Adjacent of node V1 is V2, V3 and V4.
- Adjacent of node V2 is V3, V4 and V5.
- Adjacent of node V3 is V5.
- Adjacent of node V4 is V5.
- Adjacent of node V5 is V4.

**For Graph G2:**

- Total number of edges = Total number of nodes = 9
- Degree of a node = Number of nodes for that vertex

- Degree of V1 = 3
- Degree of V2 = 3
- Degree of V3 = 1
- Degree of V4 = 1
- Degree of V5 = 1

– Outdegree of a vertex = Number of nodes of that vertex.

- Outdegree of V1 is 3
- Outdegree of V2 is 3
- Outdegree of V3 is 1
- Outdegree of V4 is 1
- Outdegree of V5 is 1

– Indegree of a vertex = Traverse all nodes and count the occurrences of that vertex.

- Indegree of V1 = The occurrence of V1 is 0 (zero) i.e. no occurrence. Hence its indegree is zero.
- Indegree of V2 = 1 (since out of 9 nodes, it is visible only once).
- Indegree of V3 = 2
- Indegree of V4 = 3
- Indegree of V5 = 3

adj[i][k] and adj[k][j] are TRUE.

which means that there is an arc from i to node k and an arc from k to node j. Thus adj[i][k] && adj[k][j] equals TRUE if an only if there is a path of length 2 from i to j passing through k.

Consider an array adj2 such that adj2[i][j] is the value of the foregoing expression. adj2 is called the path matrix of length 2. adj[i][j] indicates whether or not there is a path of length 2 between i and j. Consider the following example:

The Adjacency Matrix for the above graph is shown below:

The path matrix for the above adjacency matrix is as follows:

**Traversal schemes:**

**DFS (Depth First Search)****BFS (Breadth First Search)**

**DFS (Depth First Search): **We move depth-wise i.e. we start from the first node (any of the vertex) and then visit the node that is adjacent to it. Then from that node we move to the vertex adjacent to it and so on. If we can’t move further, then return to the previous node and continue. We keep on marking the nodes which we have already visited.

**BFS (Breadth First Search):** After a node is visited, we first visit all the vertices adjacent to it.

**Example:** Refer to the above graph. BFS traversal result is-

V1, V2, V3, V4, V5, V6, V7, V8

**Spanning trees:** Spanning trees have two basic properties-

**(i) acyclic**

**(ii) connected**

If we define a subgraph of this graph, then all such possibilities that define it as a subgraph with no cycle may be referred to as spanning tree.

If the graph is a weighted graph, then it is often desired to create a spanning tree T for G such that the sum of weights of the tree edges in T is as small as possible. Such a tree is called a minimum spanning tree and represents the cheapest way of connecting all the nodes in G.

The dotted subgraph with least weight 60 is an example of minimum cost spanning tree, as it has minimum weight value.

There are a number of techniques for creating a minimum spanning tree for a weighted graph:

**Prim’s Algorithm****Kruskal’s algorithm****Round Robin Algorithm**

An arbitrary node is chosen initially as the tree root. The nodes of the graph are then appended to the tree one at a time until all nodes of the graph are included.

In figure (a), V1 is taken to be root of the tree, which is an arbitrary node chosen as a root. Adjacent of V1 is V2 and V3. V2 becomes the right child of V1 and V3 becomes the left child of V1 in the tree as shown in fig (b).

The nodes of the graph are initially considered as n distinct partial trees with one node each. At each step of the algorithm two distinct partial trees are connected into a single partial tree by an edge of the graph. When only one partial tree exists, it is a minimum spanning tree.

The issue is what connecting arc to use at each step. The answer is to use the arc of minimum cost that connects two distinct trees. To do this, the arcs can be placed in a priority queue based on weight. The arc of lowest weight is then examined to see if it connects two distinct trees.

An ascending order priority queue is constructed that contains the weights of the arcs of the given graphs as shown in the following figure:

Any further insertion is not required since number of vertices is equal to 4, which is the same as the total number of vertices in the given graph. Thus lower weights are chosen to construct minimum spanning tree.

**3. Round Robin Algorithm (given by Tarjan & Cheriton):**

It is similar to Kruskal’s algorithm except that there is a priority queue of arcs associated with each partial tree rather than one global priority queue of all un-examined arcs.

All partial trees are maintained in the queue, Q. Associated with each partial tree T, is a priority queue. P(T) of all arcs with exactly one incident node is the tree ordered by the weights of the arcs.

/*Implementation of Adjacency List Representation for graphs*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

#include <stdlib.h>

#define MAX 4

struct edge;

typedef struct node

{

int id;

char city[25];

struct edge *next;

}VERTEX;

typedef struct edge

{

int adj;

int distance;

struct edge *next;

}EDGE;

VERTEX *nodes[MAX]={NULL};

VERTEX *makevertex(char str[],int pos)

{VERTEX *temp = (VERTEX *)malloc(sizeof(VERTEX));

temp->id = pos;

strcpy(temp->city,str);

temp->next=NULL;

return temp;

}

void getnode_info()

{

int i;

char city[25];

VERTEX *temp;

for(i=0;i<MAX;i++)

{printf(“Enter city name?”);

scanf(“%s”,city);

temp=makevertex(city,i);

nodes[i]=temp;

}

}

int return_subscript(char nm[])

{int i;

for(i=0;i<MAX;i++)

if(strcmp(nm,nodes[i]->city)==0)

return i;

return -1;

}

EDGE *makenode(int num,int j)

{

EDGE *temp = (EDGE *)malloc(sizeof(EDGE));

temp->adj=j;

temp->distance=num;

temp->next=NULL;

return temp;

}

VERTEX *join(VERTEX *node,EDGE *arc)

{VERTEX *temp=node;

EDGE *edg=temp->next;

if(node->next==NULL)

{node->next=arc;

return node;

}

while(edg->next!=NULL)

edg=edg->next;

temp->next=arc;

return node;

}

void display()

{int i;

EDGE *temp;

for(i=0;i<MAX;i++)

{printf(“%dt%st”,nodes[i]->id,nodes[i]->city);

temp=nodes[i]->next;

while(temp!=NULL)

{printf(“%d/%dt”,temp->adj,temp->distance);

temp=temp->next;

}

printf(“n”);

}

}

void main()

{

int ans,i,j;

EDGE *temp;

char city1[25],city2[25];

int distance;

getnode_info();

do

{

do

{printf(“nDoes route exists?”);

printf(“nFrom City?”);

scanf(“%s”,city1);

printf(“nTo City?”);

scanf(“%s”,city2);

i=return_subscript(city1);

j=return_subscript(city2);

}while(i==-1 || j==-1);

/*Now making node*/

printf(“nEnter distance?”);

scanf(“%d”,&distance);

temp=makenode(distance,j);

nodes[i]=join(nodes[i],temp);

printf(“nContinue(1/0)?”);

scanf(“%d”,&ans);

}while(ans==1);

display();

getch();

}

/*Program to sketch a graph using adjacency matrix*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

#define MAX 4

struct vertex

{int num;

};

struct edge

{int adj;

};

struct graph

{struct vertex nodes[MAX];

struct edge edges[MAX][MAX];

}g;

int return_subscript(int val)

{int i;

for(i=0;i<MAX;i++)

{if(g.nodes[i].num==val)

return i;

}

return -1;

}

void display()

{

int i,j;

clrscr();

printf(“t”);

for(i=0;i<MAX;i++)

printf(“%dt”,g.nodes[i].num);

printf(“n”);

for(i=0;i<MAX;i++)

{printf(“%dt”,g.nodes[i].num);

for(j=0;j<MAX;j++)

printf(“%dt”,g.edges[i][j].adj);

printf(“n”);

}

}

void main()

{

int i,j,ans,n1,n2;

clrscr();

for(i=0;i<MAX;i++)

{printf(“nEnter vertex value?”);

scanf(“%d”,&g.nodes[i].num);

}

do

{

printf(“nEnter two nodes?”);

scanf(“%d %d”,&n1,&n2);

i=return_subscript(n1);

j=return_subscript(n2);

printf(“nDoes route exists(1/0)?”);

scanf(“%d”,&g.edges[i][j].adj);

printf(“nContinue(1/0)?”);

scanf(“%d”,&ans);

}while(ans==1);

do

{

printf(“nNow enter two nodes numbers?”);

printf(“nI will tell you whether route exists or not…”);

scanf(“%d %d”,&n1,&n2);

i=return_subscript(n1);

j=return_subscript(n2);

while(i==-1 || j==-1)

{printf(“nInvalid numbers! Try Again.”);

scanf(“%d %d”,&n1,&n2);

i=return_subscript(n1);

j=return_subscript(n2);

}

if(g.edges[i][j].adj==1)

printf(“nRoute exists…”);

else

printf(“nRoute does not exists…”);

printf(“nConfirm more entries(1/0)?”);

scanf(“%d”,&ans);

}while(ans==1);

display();

getch();

}

/*Implementation of adjacency matrix*/

#include <stdio.h>

#include <conio.h>

#include <string.h>

#define MAX 4

typedef struct node

{int id;

char city[25];

}VERTEX;

typedef struct edge

{int adj;

int distance;

}EDGE;

typedef struct graph

{VERTEX v[MAX];

EDGE e[MAX][MAX];

}GRAPH;

GRAPH g;

int return_subscript(char s[]);

void main()

{

int i,j;

char city1[25],city2[25];

int ans;

clrscr();

for(i=0;i<MAX;i++)

{printf(“nEnter City?”);

scanf(“%s”,g.v[i].city);

g.v[i].id = i;

}

printf(“nNow enter information about distance if a route exists!”);

do

{do

{

printf(“nEnter city1?”);

scanf(“%s”,city1);

printf(“nEnter city2?”);

scanf(“%s”,city2);

i=return_subscript(city1);

j=return_subscript(city2);

}while(i==-1 || j==-1);

printf(“nEnter distance?”);

g.e[i][j].adj=1;

scanf(“%d”,&g.e[i][j].distance);

printf(“More entries (1/0)?”);

scanf(“%d”,&ans);

}while(ans==1);

printf(“tt”);

for(i=0;i<MAX;i++)

printf(“%dt”,g.v[i].id);

printf(“ntt”);

for(i=0;i<MAX;i++)

printf(“%st”,g.v[i].city);

printf(“nn”);

for(i=0;i<MAX;i++)

{printf(“%d %st”,g.v[i].id,g.v[i].city);

for(j=0;j<MAX;j++)

printf(“%d/%dt”,g.e[i][j].adj,g.e[i][j].distance);

printf(“n”);

}

getch();

}

int return_subscript(char str[])

{int i;

for(i=0;i<MAX;i++)

if(strcmp(g.v[i].city,str)==0)

return i;

return -1;

}