You are on page 1of 20

1| Page

DOA: - 06/04/11
DOS: - 23/04/11

DESIGN PROBLEM: - 02
“ANALYSIS AND DESIGN
OF” “ALGORITHMS”
(CSE-458)

SUBMITTED TO: Mr. ROHAN SHARMA


SUBMITTED BY:

Lokesh tiwari

REG No: 5050070011

ROLL NO: RA17E1A03


2| Page

SECTION: A17E1

B.TECH (HONS) CSE (8th SEM)

DESIGN PROBLEM
Design Directed Graph with the required nodes and edges graphically in
C or C++. Give the functional operations like addition, deletion of nodes
and edges. Display number of comparison in each operation. Also
implement all Graph traversal approaches (pre order, post order, in
order etc.) graphically.

SOLUTION
DIRECTED GRAPH: - A graph in which each graph edge is replaced by a
directed graph edge, also called a digraph. A directed graph having no multiple
edges or loops (corresponding to a binary adjacency matrix with 0s on the diagonal) is
called a simple directed graph. A complete graph in which each edge is bidirected is called
a complete directed graph. A directed graph having no symmetric pair of directed edges
(i.e., no bidirected edges) is called an oriented graph. A complete oriented graph (i.e., a
directed graph in which each pair of nodes is joined by a single edge having a unique
direction) is called a tournament.
3| Page

IN OTHER WORDS: A directed graph or digraph is a pair G = (V, A) (sometimes G =


(V, E)) of:

• A set V, whose elements are called vertices or nodes,


• A set A of ordered pairs of vertices, called arcs, directed edges, or arrows (and
sometimes simply edges with the corresponding set named E instead of A).

************************************************************************
******

Program in c to make a directed graph and perform operations addition,


deletion of nodes and edges: -

#include<stdio.h>
struct edge;
struct node
{
struct node *next;
char name;
struct edge *adj;
}
*start = NULL;
struct edge
{
char dest;
struct edge *link;
};
struct node *find( char item );
main()
{
int choice;
char node, origin, destin;
while ( 1 )
{
printf( "1.Insert a node\n" );
printf( "2.Insert an edge\n" );
printf( "3.Delete a node\n" );
4| Page

printf( "4.Delete an edge\n" );


printf( "5.Display\n" );
printf( "6.Exit\n" );
printf( "Enter your choice : " );
scanf( "%d", &choice );
switch ( choice )
{
case 1:
printf( "Enter a node to be inserted : " );
fflush( stdin );
scanf( "%c", &node );
insert_node( node );
break;
case 2:
printf( "Enter an edge to be inserted : " );
fflush( stdin );
scanf( "%c %c", &origin, &destin );
insert_edge( origin, destin );
break;
case 3:
printf( "Enter a node to be deleted : " );
fflush( stdin );
scanf( "%c", &node );
delete_node( node );
delnode_edge( node );
break;
case 4:
printf( "Enter an edge to be deleted : " );
fflush( stdin );
scanf( "%c %c", &origin, &destin );
del_edge( origin, destin );
break;
case 5:
display();
break;
case 6:
exit();
default:
5| Page

printf( "Wrong choice\n" );


break;
}
}
}
insert_node( char node_name )
{
struct node * tmp, *ptr;
tmp = malloc( sizeof( struct node ) );
tmp->name = node_name;
tmp->next = NULL;
tmp->adj = NULL;
if ( start == NULL )
{
start = tmp;
return ;
}
ptr = start;
while ( ptr->next != NULL )
ptr = ptr->next;

ptr->next = tmp;
}
delete_node( char u )
{
struct node * tmp, *q;
if ( start->name == u )
{
tmp = start;
start = start->next;
free( tmp );
return ;
}
q = start;
while ( q->next->next != NULL )
{
if ( q->next->name == u )
{
6| Page

tmp = q->next;
q->next = tmp->next;
free( tmp );
return ;
}
q = q->next;
}
if ( q->next->name == u )
{
tmp = q->next;
free( tmp );
q->next = NULL;
}
}
delnode_edge( char u )
{
struct node * ptr;

struct edge *q, *start_edge, *tmp;


ptr = start;

while ( ptr != NULL )


{
if ( ptr->adj->dest == u )
{
tmp = ptr->adj;
ptr->adj = ptr->adj->link;
free( tmp );
continue; }
q = ptr->adj;
while ( q->link->link != NULL )
{
if ( q->link->dest == u )
{
tmp = q->link;
q->link = tmp->link;
free( tmp );
continue;
7| Page

q = q->link;
}

if ( q->link->dest == u )
{
tmp = q->link;
free( tmp );
q->link = NULL;
}

ptr = ptr->next;
}
}

insert_edge( char u, char v )


{
struct node * locu, *locv;
struct edge *ptr, *tmp;
locu = find( u );
locv = find( v );

if ( locu == NULL )
{
printf( "Source node not present ,first insert node %c\n", u );
return ;
}

if ( locv == NULL )
{
printf( "Destination node not present ,first insert node %c\n", v );
return ;
}

tmp = malloc( sizeof( struct edge ) );


tmp->dest = v;
tmp->link = NULL;

if ( locu->adj == NULL ) /* item added at the begining */


8| Page

{
locu->adj = tmp;
return ;
}

ptr = locu->adj;
while ( ptr->link != NULL )
ptr = ptr->link;

ptr->link = tmp;

}
struct node *find( char item )
{

struct node *ptr, *loc;


ptr = start;

while ( ptr != NULL )


{
if ( item == ptr->name )
{
loc = ptr;
return loc;
}
else
ptr = ptr->next;
}

loc = NULL;
return loc;
}
del_edge( char u, char v )
{

struct node * locu, *locv;

struct edge *ptr, *tmp, *q;


9| Page

locu = find( u );

if ( locu == NULL )
{
printf( "Source node not present\n" );
return ;
}

if ( locu->adj->dest == v )
{
tmp = locu->adj;
locu->adj = locu->adj->link;
free( tmp );
return ;
}

q = locu->adj;

while ( q->link->link != NULL )


{
if ( q->link->dest == v )
{
tmp = q->link;
q->link = tmp->link;
free( tmp );
return ;
}

q = q->link;
}
if ( q->link->dest == v )
{
tmp = q->link;
free( tmp );
q->link = NULL;
return ;
}
10 | P a g e

printf( "This edge not present in the graph\n" );


} /*End of del_edge()*/

display()
{

struct node * ptr;

struct edge *q;

ptr = start;

while ( ptr != NULL )


{
printf( "%c ->", ptr->name );
q = ptr->adj;

while ( q != NULL )
{
printf( " %c", q->dest );
q = q->link;
}

printf( "\n" );
ptr = ptr->next;
}
}

************************************************************************
******

Traversal approaches:
Tree-traversal refers to the process of visiting (examining and/or updating) each node in
a tree data structure, exactly once, in a systematic way. Such traversals are classified by
the order in which the nodes are visited. The following algorithms are described for
a binary tree, but they may be generalized to other trees as well.
11 | P a g e

There are two fundamentally different kinds of binary trees traversals-

• Depth-first

• Breadth-first.

When we watch the animation, notice that the path followed by each of these traversals is
along the branches of the tree. Each node of the tree is visited three times during each of
the depth-first traversals, once on its way down the tree, a second time coming up from the
left child, and a third time coming up from the right child. When you watch the animation
of these traversals, notice that a checkmark is placed beneath each node each time the
node is visited.

There are three different types of depth-first traversals

• Preorder

• Inorder

• Postorder
(1) Preorder:

The first depth-first traversal method we consider is called preorder traversal . Preorder
traversal is defined recursively as follows. To do a preorder traversal of a general tree:

1. Visit the root first; and then


2. Do a preorder traversal each of the subtrees of the root one-by-one in the
order given.

Preorder traversal gets its name from the fact that it visits the root first. In the case of a
binary tree, the algorithm becomes:

1. Visit the root first; and then


2. traverse the left subtree; and then
3. traverse the right subtree.
12 | P a g e

Preorder implementation:

preorder(tree)
begin
if tree is null, return;

print(tree.root);
preorder(tree.left_subtree);
preorder(tree.right_subtree);
end
Preorder example:

Preorder traversal sequence: (root, left, right)

F, B, A, D, C, E, G, I, H

Preorder

Push F
pop F
push GB
pop B
13 | P a g e

push DA
pop A
pop D
push EC
pop C
pop E
pop G
push I

************************************************************************
******

(2) Inorder Traversal :


The third depth-first traversal method is inorder traversal . Inorder traversal only makes
sense for binary trees. Whereas preorder traversal visits the root first and postorder
traversal visits the root last, inorder traversal visits the root in between visiting the left and
right subtrees:

1. Traverse the left subtree; and then


2. visit the root; and then
3. Traverse the right subtree.

Inorder Implementation:

InOrder (TreeNode<T> n)
{
if (n != null)
{
InOrder (n.getLeft());
visit (n)
InOrder (n.getRight());
}
}

Inorder example

Inorder traversal sequence: (left, root, right)


14 | P a g e

A, B, C, D, E, F, G, H, I

Push F B A
pop A
pop B
push D C
pop C
pop D
push E
pop E
pop F
push G
pop G
push I H
pop H
pop I

(3) Postorder Traversal:

The second depth-first traversal method we consider is postorder traversal . In contrast


with preorder traversal, which visits the root first, postorder traversal visits the root last.
To do a postorder traversal of a general tree:

1. Do a postorder traversal each of the subtrees of the root one-by-one in the


order given; and then
2. Visit the root.
15 | P a g e

To do a postorder traversal of a binary tree

1. Traverse the left subtree; and then


2. traverse the right subtree; and then
3. Visit the root.

Postorder Implementation:

Algorithm

stack-postorder(T, v)
establish stack S;
S.push(v)
while (S in not empty)
do
{
u := S.top();
if (u is leaf or marked)
then
{
visit u;
S.pop();
}
Else
mark the top element of S;
let u1, u2, …, un be the children of u;
for (j = n; j >= 1; j--)
S.push(uj);
}
}

Postorder Example: -
16 | P a g e

Postorder traversal sequence: (left, right, root)

A, C, E, D, B, H, I, G, F

Push F B A
Pop A
Push D C
Pop C
Push D C
Pop C
Push E
Pop E
Pop D
Pop B
Push G I H
Pop H
Pop I
Pop G
Pop F

************************************************************************
******

Program in c for implementing inorder, postorder and preorder


traversals: -
17 | P a g e

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *lchild,*rchild;
};

struct tree *insert(struct tree *p,int n);


void inorder(struct tree *p);
void preorder(struct tree *p);
void postorder(struct tree *p);
void main()
{
int x,y,i;
struct tree *root;
clrscr();
root=NULL;
printf("Enter the no. of nodes in the tree\n");
scanf("%d",&x);
while(x-->0)
{
printf("Enter the data part of the node\n");
scanf("%d",&y);
root=insert(root,y);
}
clrscr();
printf("\t\tEnter the traversal u want\n");
printf("1.Inorder.\n2.Preorder.\n3.Postorder.\n");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("The inorder traversal is \n");
inorder(root);
}
18 | P a g e

break;
case 2:
{
printf("The preorder traversal is\n");
preorder(root);
}
break;
case 3:
{
printf("The postorder traversal is\n");
postorder(root);
}
break;
}
getch();
}
struct tree *insert(struct tree *p,int n)
{
static struct tree *temp1,*temp2;
if(p==NULL)
{
p=(struct tree *)malloc(sizeof(struct tree));
p->data=n;
p->lchild=p->rchild=NULL;
}
else
{
temp1=p;
while(temp1!=NULL)
{
temp2=temp1;
if(n<temp1->data)
temp1=temp1->lchild;
else
temp1=temp1->rchild;
}
if(temp2->data>n)
{
19 | P a g e

temp2->lchild=(struct tree *)malloc(sizeof(struct tree));


temp2=temp2->lchild;
temp2->data=n;
temp2->lchild=temp2->rchild=NULL;
}
else
{
temp2->rchild=(struct tree *)malloc(sizeof(struct tree));
temp2=temp2->rchild;
temp2->data=n;
temp2->lchild=temp2->rchild=NULL;
}
}
return p;
}
void inorder(struct tree *p)
{
if(p!=NULL)
{
inorder(p->lchild);
printf("%d ",p->data);
inorder(p->rchild);
}
}
void preorder(struct tree *p)
{
if(p!=NULL)
{
printf("%d ",p->data);
preorder(p->lchild);
preorder(p->rchild);
}
}
void postorder(struct tree *p)
{
if(p!=NULL)
{
postorder(p->lchild);
20 | P a g e

postorder(p->rchild);
printf("%d ",p->data);
}
}

************************************************************************
************************************************************************
************

You might also like