Design Problem: - 02: "Analysis and Design Of" "Algorithms" (CSE-458)
Design Problem: - 02: "Analysis and Design Of" "Algorithms" (CSE-458)
Design Problem: - 02: "Analysis and Design Of" "Algorithms" (CSE-458)
DOA: - 06/04/11
DOS: - 23/04/11
DESIGN PROBLEM: - 02
“ANALYSIS AND DESIGN
OF” “ALGORITHMS”
(CSE-458)
Lokesh tiwari
SECTION: A17E1
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
************************************************************************
******
#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
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;
q = q->link;
}
if ( q->link->dest == u )
{
tmp = q->link;
free( tmp );
q->link = NULL;
}
ptr = ptr->next;
}
}
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 ;
}
{
locu->adj = tmp;
return ;
}
ptr = locu->adj;
while ( ptr->link != NULL )
ptr = ptr->link;
ptr->link = tmp;
}
struct node *find( char item )
{
loc = NULL;
return loc;
}
del_edge( char u, char v )
{
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;
q = q->link;
}
if ( q->link->dest == v )
{
tmp = q->link;
free( tmp );
q->link = NULL;
return ;
}
10 | P a g e
display()
{
ptr = start;
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
• 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.
• 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:
Preorder traversal gets its name from the fact that it visits the root first. In the case of a
binary tree, the algorithm becomes:
Preorder implementation:
preorder(tree)
begin
if tree is null, return;
print(tree.root);
preorder(tree.left_subtree);
preorder(tree.right_subtree);
end
Preorder example:
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
************************************************************************
******
Inorder Implementation:
InOrder (TreeNode<T> n)
{
if (n != null)
{
InOrder (n.getLeft());
visit (n)
InOrder (n.getRight());
}
}
Inorder example
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
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
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
************************************************************************
******
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct tree
{
int data;
struct tree *lchild,*rchild;
};
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
postorder(p->rchild);
printf("%d ",p->data);
}
}
************************************************************************
************************************************************************
************