Download - Advanced Data Structures Lab Manual
-
5/26/2018 Advanced Data Structures Lab Manual
1/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 1
INDEX
S. No. PROGRAM Page No.
1 Graph Search 2
2 Breadth First Search 5
3 Depth First Search 8
4 Dijikstras Shortest Path Algorithm 12
5 Topological Sorting 18
6 Hill Climbing 22
7 Merge Sort 28
8 Quick Sort 33
9 Producer Consumer 36
10 Concurrency List 39
11 Concurrency Stack 44
12 Concurrency Queue 49
13 Dynamic Programming 54
14 Randomized Algorithm 58
15 Dining Philosophers Problem 61
-
5/26/2018 Advanced Data Structures Lab Manual
2/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 2
GRAPH SEARCH
ALGORITHM:
Step 1: Start
Step 2: Get an input graph, source node S, node to search N
Step 3: Add S to notHandled queue
Step 4: Take first node, U from notHandled
Step 4.1: If U is equal to N
Step 4.1.1: Return
Step 4.2: Else
Step 4.2.1: Add the child of U to notHandled
Step 4.2.2: Move U from notHandled to Handled queue
Step 5: Repeat step 4 till notHandled is not empty
Step 6: Return
Step 7: End the Algorithm
JAVA CODE
importjava.util.*;
publicclassGraphSearch
{
publicstaticvoidmain(String[] arg)
{ArrayList start = new ArrayList();
ArrayList end = newArrayList();
ArrayList nodes = newArrayList();
ArrayList notHandled = newArrayList();
ArrayList handled = newArrayList();
intedgeCount, i, hSize, nSize;
String node;
Scanner s= newScanner(System.in);
System.out.println( "NOTE: The first node will be taken as source");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
3/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 3
// get FROM node and TO node for each edge
System.out.println( "Enter the FROM node:");
start.add(s.next());
System.out.println( "Enter the TO node:");end.add(s.next());
while(start.get(i).equalsIgnoreCase(end.get(i)))
{
end.remove(i);
System.out.println( "Error: SAME AS START NODE");
System.out.println( "Enter the TO node:");
end.add(s.next());
}
if(!(nodes.contains(start.get(i))))
nodes.add(start.get(i));
if(!(nodes.contains(end.get(i))))
nodes.add(end.get(i));
}
nSize=nodes.size();
System.out.println( "Enter the node to be found:");
node=s.next();
notHandled.add(start.get(0)); // root node
handled.add(notHandled.get(0));
if(node.equalsIgnoreCase(notHandled.get(0)))
hSize=nSize;
else
hSize=handled.size();
nodes.remove(notHandled.get(0));
notHandled.remove(0);
// till all nodes are handled
while(hSize
-
5/26/2018 Advanced Data Structures Lab Manual
4/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 4
a
b c
ed
System.out.println("Nodes Not Handled = "+nodes);
}
}
GRAPH
OUTPUT
NOTE: The first node will be taken as source
Enter the number of edges:
4
Enter the FROM node:
a
Enter the TO node:
b
Enter the FROM node:
a
Enter the TO node:
c
Enter the FROM node:
b
Enter the TO node:
d
Enter the FROM node:b
Enter the TO node:
e
Enter the node to be found:
c
Nodes Handled = [a, b]
Node c Found !!!
Nodes Not Handled = [d, e]
RESULT Thus the code in written in Java and executed for finding a node in a graph.
-
5/26/2018 Advanced Data Structures Lab Manual
5/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 5
BREADTH FIRST SEARCH
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G, source node S
Step 3: Add S to notHandled queue
Step 4: Take first node, U from notHandled
Step 4.1: Add the child of U to notHandled
Step 4.2: Move U from notHandled to Handled queue
Step 5: Repeat step 4 till notHandled is not empty
Step 6: Return
Step 7: End the Algorithm
JAVA CODE
importjava.util.*;publicclassBreadthFirstSearch
{
publicstaticvoidmain(String[] arg)
{
ArrayList start = new ArrayList();
ArrayList end = newArrayList();
ArrayList nodes = newArrayList();
ArrayList notHandled = newArrayList();
ArrayList handled = newArrayList();
intedgeCount, i, hSize;
Scanner s= newScanner(System.in);System.out.println( "NOTE: The first node will be taken as source");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
6/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 6
System.out.println( "Enter the TO node:");
end.add(s.next());
}
if(!(nodes.contains(start.get(i))))nodes.add(start.get(i));
if(!(nodes.contains(end.get(i))))
nodes.add(end.get(i));
}
notHandled.add(start.get(0)); // root node
System.out.println("Handled Not Handled");
System.out.println("[] "+ notHandled);
handled.add(notHandled.get(0));
hSize=handled.size();
// till all nodes are handled
while(notHandled.size()>0)
{
notHandled.remove(0);
// for the rear node in handled queue, add its children in notHandled queue
for(i=0; i0)
if(!(handled.contains(notHandled.get(0))))
handled.add(notHandled.get(0));
hSize=handled.size();
}
System.out.println("All Nodes are traversed in BFS");
}
}
GRAPH
OUTPUT
NOTE: The first node will be taken as source
Enter the number of edges:
4
Enter the FROM node:
a
Enter the TO node:b
a
b d
ec
-
5/26/2018 Advanced Data Structures Lab Manual
7/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 7
Enter the FROM node:
b
Enter the TO node:c
Enter the FROM node:
a
Enter the TO node:
d
Enter the FROM node:
d
Enter the TO node:
e
Handled Not Handled
[] [a]
[a] [b, d]
[a, b] [d, c]
[a, b, d] [c, e]
[a, b, d, c] [e]
[a, b, d, c, e] []
All Nodes are traversed in BFS
RESULT Thus the code is written in Java and executed for implementing Breadth First Search.
-
5/26/2018 Advanced Data Structures Lab Manual
8/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 8
DEPTH FIRST SEARCH
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G, source node S
Step 3: Push S to notHandled stack
Step 4: Pop top node, U from notHandled
Step 4.1: Push the child of U to notHandled
Step 4.2: Pop U from notHandled stack and add to Handled queue
Step 5: Repeat step 4 till notHandled is not empty
Step 6: Return
Step 7: End the Algorithm
JAVA CODE
importjava.util.*;
publicclassDepthFirstSearch
{
staticArrayList start= new ArrayList();
staticArrayList end= newArrayList();
staticString[] stack= newString[10];
staticArrayList handled= newArrayList();
staticArrayList notHandled= newArrayList();
staticArrayList child= newArrayList();
staticintedgeCount,top=-1;
publicstaticvoidmain(String[] arg){
Scanner s= newScanner(System.in);
System.out.println( "NOTE: The first node is assumed as ROOT");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(inti=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
9/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 9
{
end.remove(i);
System.out.println( "Error: SAME AS START NODE");
System.out.println( "Enter the TO node:");end.add(s.next());
}
}
System.out.println("Handled STACK");
check(start.get(0));
printStack();
System.out.println("All Nodes are traversed in DFS");
}
staticvoidcSort()
{
//since the child nodes are inserted in reverse order, reverse it
for(intq=child.size();q>0;q--)
push(child.get(q-1));
}
staticvoidpush(String node)
{
// add node to stack and notHandled
if(!(notHandled.contains(node)) && !(handled.contains(node)))
{
top++;
stack[top]=node;
notHandled.add(node);
}
}
staticvoidpop()
{
//move the top node from notHandled to handled
if(top>-1)
{
handled.add(stack[top]);
notHandled.remove(stack[top]);
top--;
}
}
staticvoidcheck(String node)
{
// insert the parent node in notHandled node
push(node);
printStack();
intflag=1;
for(intq=0; q
-
5/26/2018 Advanced Data Structures Lab Manual
10/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 10
a
b c
ed
pop();
flag=0;
}
//since they are not arranged, use child array ad temp arraychild.add(end.get(q));
}
//sort the child of current top of stack
cSort();
if(flag==1) // if no child nodes, then pop the top of stack
pop();
if(top>-1) //recurse the check function with new top of stack
check(stack[top]);
}
staticvoidprintStack()
{
//print the notHandled node in reverse order, since its stack
ArrayList sort = newArrayList();
for(intq=notHandled.size();q>0;q--)
sort.add(notHandled.get(q-1));
System.out.println(handled+" "+sort);
}
}
GRAPH
OUTPUT
NOTE: The first node is assumed as ROOT
Enter the number of edges:
4
Enter the FROM node:
a
Enter the TO node:b
Enter the FROM node:
a
Enter the TO node:
c
Enter the FROM node:
b
Enter the TO node:
e
Enter the FROM node:
c
Enter the TO node:
d
Handled STACK
-
5/26/2018 Advanced Data Structures Lab Manual
11/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 11
[] [a]
[a] [b, c]
[a, b] [e, c]
[a, b, e] [c][a, b, e, c] [d]
[a, b, e, c, d] []
All Nodes are traversed in DFS
RESULT Thus the code is written in Java and executed for implementing Depth First Search.
-
5/26/2018 Advanced Data Structures Lab Manual
12/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 12
DIJIKSTRAS SHORTEST PATH
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G, source node S with cost of each edge c
Step 3: Assign path of each node as NIL
Step 4: Assign distance of each node except S as INFINITY
Step 5: Assign distance of S as 0
Step 6: Add S to notHandled
Step 7: Sort the nodes based on the distance
Step 8: Choose the node U, with minimum distance
Step 8.1: Add all children of U to notHandled
Step 8.2: For all the child nodes of U
Step 8.2.1: Assign path as node U
Step 8.2.2: If distance of current node greater than d(U) + c
Step 8.2.2.1: Assign distance as d(U) + c
Step 8.3: Move U from notHandled to Handled
Step 9: Go to step 7 if notHandled is not empty
Step 10: End Algorithm
JAVA CODE
importjava.util.*;
publicclassShortestPath_Dijikstra
{
staticArrayList start= new ArrayList();
staticArrayList end= newArrayList();
staticArrayList cost= newArrayList();
staticArrayList nodes= newArrayList();staticString[]path= newString[20];
staticint[] length= newint[20];
-
5/26/2018 Advanced Data Structures Lab Manual
13/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 13
staticArrayList handled= newArrayList();
staticArrayList notHandled= newArrayList();
staticintedgeCount;
publicstaticvoidmain(String[] arg)
{
intnode, i;
Scanner s= newScanner(System.in);
// Get start node, end node, cost of each edge from user
System.out.println( "NOTE: The first node will be taken as source");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
14/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 14
System.out.println(" ");
notHandled.clear();
handled.add(nodes.get(node));
}System.out.println("NODE KNOWN LENGTH PATH");
print();
System.out.println("");
}
staticintfindMinimum()
{
intcost=1000;
intnode=0;
for(intk=0; klength[k] && !(handled.contains(nodes.get(k))))
{
cost=length[k];
node=k;
}
returnnode;
}
staticvoidfindChild(intnode)
{
for(intj=0;j
-
5/26/2018 Advanced Data Structures Lab Manual
15/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 15
3
5
72
6
4
p
q r
ts
return0;
}
}
GRAPH
OUTPUT
NOTE: The first node will be taken as source
Enter the number of edges:
6Enter the FROM node:
p
Enter the TO node:
q
Enter the cost of edge:
5
Enter the FROM node:
p
Enter the TO node:
r
Enter the cost of edge:
4Enter the FROM node:
q
Enter the TO node:
s
Enter the cost of edge:
5
Enter the FROM node:
q
Enter the TO node:
t
Enter the cost of edge:
3Enter the FROM node:
r
-
5/26/2018 Advanced Data Structures Lab Manual
16/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 16
Enter the TO node:
s
Enter the cost of edge:
2Enter the FROM node:
r
Enter the TO node:
t
Enter the cost of edge:
6
No. of nodes=5
NODE KNOWN LENGTH PATH
p 0 0 NIL
q 0 1000 null
r 0 1000 null
s 0 1000 null
t 0 1000 null
NODE KNOWN LENGTH PATH
p 0 0 NIL
q 0 5 p
r 0 4 p
s 0 1000 null
t 0 1000 null
NODE KNOWN LENGTH PATH
p 1 0 NIL
q 0 5 p
r 0 4 p
s 0 6 r
t 0 10 r
NODE KNOWN LENGTH PATH
p 1 0 NIL
q 0 5 p
r 1 4 p
s 0 6 r
t 0 8 q
NODE KNOWN LENGTH PATH
p 1 0 NIL
q 1 5 p
r 1 4 p
s 0 6 r
t 0 8 q
NODE KNOWN LENGTH PATH
p 1 0 NIL
q 1 5 p
r 1 4 p
s 1 6 rt 0 8 q
-
5/26/2018 Advanced Data Structures Lab Manual
17/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 17
NODE KNOWN LENGTH PATH
p 1 0 NIL
q 1 5 pr 1 4 p
s 1 6 r
t 1 8 q
RESULT Thus the code is written in Java and executed for implementing Dijikstras Shortest Path
Algorithm.
-
5/26/2018 Advanced Data Structures Lab Manual
18/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 18
TOPOLOGICAL SORT
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G
Step 3: Find the in-degree of each node in graph G
Step 4: Sort the nodes with respect to their in-degrees
Step 5: Remove the node with in-degree 0 from graph
Step 6: Add the node to Handled queue
Step 7: Go to step 3 if graph is not empty
Step 8: Return Handled
Step 9: End Algorithm
JAVA CODE
packageGraph;importjava.util.*;
publicclassTopologicalSort
{
staticArrayList start= new ArrayList();
staticArrayList end= newArrayList();
staticArrayList nodes= newArrayList();
staticint[] indegree= newint[20];
staticArrayList handled= newArrayList();
staticintedgeCount, i, nodeSize, k,j, count,pNodeSize;
staticbooleanflag=true;
publicstaticvoidmain(String[] arg)
{
intmin;
Scanner s= newScanner(System.in);
System.out.println( "NOTE: The first node will be taken as source");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
19/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 19
while(start.get(i).equalsIgnoreCase(end.get(i)))
{
end.remove(i);//to remove node added before checking error
System.out.println( "Error: SAME AS START NODE");System.out.println( "Enter the TO node:");
end.add(s.next());
}
if(!(nodes.contains(start.get(i))))
nodes.add(start.get(i));
if(!(nodes.contains(end.get(i))))
nodes.add(end.get(i));
}
nodeSize=nodes.size();
for(k=0;k
-
5/26/2018 Advanced Data Structures Lab Manual
20/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 20
a
eb
cd
}
}
staticintminDegree(){
intmin=indegree[0],index=0;
for(i=0;iindegree[i])
{
min=indegree[i];
index=i;
}
}
returnindex;
}
staticbooleancheck()
{
for(i=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
21/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 21
c
Enter the TO node:
d
Enter the FROM node:c
Enter the TO node:
e
Enter the FROM node:
a
Enter the TO node:
d
indegree of a is 0
indegree of b is 1
indegree of c is 1
indegree of d is 2
indegree of e is 1
Sorted queue [a]
-----------------------------------
indegree of b is 0
indegree of c is 1
indegree of d is 1
indegree of e is 1
Sorted queue [a, b]
-----------------------------------
indegree of c is 0
indegree of d is 1
indegree of e is 1
Sorted queue [a, b, c]
-----------------------------------
indegree of d is 0
indegree of e is 0
Sorted queue [a, b, c, d]
-----------------------------------
indegree of e is 0
Sorted queue [a, b, c, d, e]
-----------------------------------
RESULT Thus the code is written in Java and executed for implementing Topological sorting.
-
5/26/2018 Advanced Data Structures Lab Manual
22/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 22
HILL CIMBING
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G, source node S, destination node D with flow_capacity of each edge,
rate of flow
Step 3: Find the various paths from source and destination
Step 4: Select a path from set of path
Step 4.1: Find min_cut of selected path
Step 4.2: Netflow is the sum of min_cut of all paths
Step 5: If the netFlow > rate of flow then
Step 5.1: Select a path from set of path
Step 5.2: Find min_cut of selected path
Step 5.3: For each edge of the path
Step 5.3.1: Calculate flow_capacitymin_cut
Step 5.3.2: Assign the result to flow_capacity of the edge
Step 5.3.3: Calculate and assign netFlow = netFlowmin_cut
Step 5.4: Goto Step 5.1 if netFlow > 0
Step 5.5: Else Return the flow through each edge of the graph
Step 6: Else Exit
Step 7: End Algorithm
JAVA CODE
importjava.util.*;
publicclassHillClimbing
{
staticArrayList start= new ArrayList();
staticArrayList end= newArrayList();
staticArrayList nodes= newArrayList();
staticArrayList capacity= newArrayList();
staticArrayList stack= newArrayList();
staticint[]flow= newint[20];
-
5/26/2018 Advanced Data Structures Lab Manual
23/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 23
staticString dest,source;
staticintedgeCount,i,j,netFlow,flag=0,top,netCapacity=0,capa;
publicstaticvoidmain(String[] arg)
{Scanner s= newScanner(System.in);
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
24/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 24
else
queue.add(end.get(i));
}
while(!queue.isEmpty()){
findPath(queue.get(0));
stack.remove(stack.size()-1);
queue.remove(0);
}
}
staticvoidpathCapacity(ArrayList path)
{
intmin=1000;
ArrayList edges = newArrayList();
for(j=0;jmin)
{
netFlow=netFlow-min;
capa=capa+min;
System.out.println("Flow along the PATH "+stack+" with FLOW "+min);
for(intq=0;q
-
5/26/2018 Advanced Data Structures Lab Manual
25/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 25
8
16
7
12 42
17
15
10b
13
a
e
c
d
ts
}
}
}
staticvoidcheck()
{
if(netFlow>0)
{
System.out.println("The Maximum flow of the network is "+capa);
System.out.println("The requested flow cant be accomodated.."+netFlow);
}
}
staticvoidprint()
{
System.out.println(" ");
for(j=0;j
-
5/26/2018 Advanced Data Structures Lab Manual
26/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 26
Enter the FROM node:
s
Enter the TO node:
cEnter the capacity of the edge:
12
Enter the FROM node:
d
Enter the TO node:
e
Enter the capacity of the edge:
16
Enter the FROM node:
d
Enter the TO node:
b
Enter the capacity of the edge:
7
Enter the FROM node:
a
Enter the TO node:
b
Enter the capacity of the edge:
15
Enter the FROM node:
e
Enter the TO node:
c
Enter the capacity of the edge:
8
Enter the FROM node:
b
Enter the TO node:
c
Enter the capacity of the edge:
17
Enter the FROM node:
c
Enter the TO node:
t
Enter the capacity of the edge:
42
Enter the Source Node
s
Enter the Destination Node
t
Enter the Flow/Rate of the network
35
Flow along the PATH [s, a, b, c, t] with FLOW 10
Flow along the PATH [s, d, e, c, t] with FLOW 8
Flow along the PATH [s, d, b, c, t] with FLOW 5
Flow along the PATH [s, c, t] with FLOW 12
Flow across the edge s - a is : 10/10
-
5/26/2018 Advanced Data Structures Lab Manual
27/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 27
Flow across the edge s - d is : 13/13
Flow across the edge s - c is : 12/12
Flow across the edge d - e is : 8/16
Flow across the edge d - b is : 5/7Flow across the edge a - b is : 10/15
Flow across the edge e - c is : 8/8
Flow across the edge b - c is : 15/17
Flow across the edge c - t is : 35/42
RESULT Thus the code is written in Java and executed for implementing Hill Climbing.
-
5/26/2018 Advanced Data Structures Lab Manual
28/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 28
MERGE SORT
ALGORITHM:
Step 1: Start
Step 2: Get n inputs elements
Step 3: If the number of elements >2 then
Step 3.1: Split the elements into 2 halves, right and left using the index of the element
Step 3.2: Recurse the Step 3 with right array
Step 3.3: Recurse the Step 3 with left array
Step 3.4: Call merge function
Step 4: Else if the number of elements ==2 then
Step 4.1: Add first element to right array n second element to left array
Step 4.2: Call the merge function
Step 5: In the merge function
Step 5.1: Compare each element of the right and left array
Step 5.2: Add the sorted list to a global array
Step 6: End Algorithm
JAVA CODE
importjava.util.*;
publicclassMergeSort{
staticintn,i;
staticArrayList element= newArrayList();
staticArrayList sorted= newArrayList();
publicstaticvoidmain(String[] arg)
{
Scanner s= newScanner(System.in);
System.out.println( "NOTE: Duplicate terms will be removed");
System.out.println( "Enter the number of elements to be sorted:");
n=s.nextInt();
for(i=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
29/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 29
element.add(s.nextInt());
}
splitting(element);
System.out.println(" ");System.out.println("Sorted List of given List is : "+sorted);
}
staticvoidsplitting(ArrayList entire)
{
intsize=entire.size()/2;
ArrayList right = newArrayList();
ArrayList left = newArrayList();
// if n>2 then recurse
if(entire.size()>2)
{
//divide into 2
for(i=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
30/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 30
if(!merged.contains(left.get(y)))
merged.add(left.get(y));
y++;
i++;}
elseif(ls==y && rs==x && ss>z)//only sort
{
if(!merged.contains(sorted.get(z)))
merged.add(sorted.get(z));
z++;
i++;
}
elseif(ls>y && rs==x && ss>z)//left n sort
{
if(left.get(y)x && ss>z)//right n sort
{
if(right.get(x)y && rs>x && ss==z)//right n left
{
if(right.get(x)
-
5/26/2018 Advanced Data Structures Lab Manual
31/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 31
}
else
{
if(!merged.contains(left.get(y)))merged.add(left.get(y));
y++;
i++;
}
}
elseif(ls>y && rs>x && ss>z)//right, sort n left
{
if(left.get(y)
-
5/26/2018 Advanced Data Structures Lab Manual
32/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 32
System.out.println(" ");
sorted=merged;
}
}
OUTPUT
NOTE: Duplicate terms will be removed
Enter the number of elements to be sorted:
5
Enter the element 1 :
1
Enter the element 2 :
3Enter the element 3 :
5
Enter the element 4 :
2
Enter the element 5 :
4
Left : [3]
Previously Sorted : []
Right : [1]
-----------------------------
Locally Merged Sorted List : [1, 3]
Left : [4]
Previously Sorted : [1, 3]
Right : [2]
-----------------------------
Locally Merged Sorted List : [1, 2, 3]
Left : [2, 4]
Previously Sorted : [1, 2, 3]
Right : [5]
-----------------------------
Locally Merged Sorted List : [1, 2, 3, 4, 5]
Left : [5, 2, 4]
Previously Sorted : [1, 2, 3, 4, 5]
Right : [1, 3]
-----------------------------
Locally Merged Sorted List : [1, 2, 3, 4, 5]
Sorted List of given List is : [1, 2, 3, 4, 5]
RESULT Thus the code is written in Java and executed for implementing Merge Sort.
-
5/26/2018 Advanced Data Structures Lab Manual
33/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 33
QUICK SORT
ALGORITHM:
Step 1: Start
Step 2: Get n elements to be sorted
Step 3: If element size>2
Step 3.1: Find the average of n elements, pivot
Step 3.2: if element lesser than pivot
Step 3.2.1: Add element to lesser queue
Step 3.2.2: Recurse step 3 with lesser queue
Step 3.3: else
Step 3.3.1: Add element to bigger queue
Step 3.3.2: Recurse step 3 with bigger queue
Step 4: else
Step 4.1: if element[0] < element[1]
Step 4.1.1: Swap elements
Step 4.1.2: Join lesser and bigger list continuously
Step 6: End Algorithm
JAVA CODE
importjava.util.*;
publicclassQuickSort
{
staticintn,i;
staticArrayList element= newArrayList();
staticArrayList sorted= newArrayList();
publicstaticvoidmain(String[] arg)
{
Scanner s= newScanner(System.in);
//get elements
System.out.println( "Enter the number of elements to be sorted:");
n=s.nextInt();
for(i=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
34/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 34
System.out.println( "Enter the element "+(i+1)+" :");
element.add(s.nextInt());
}
if(n>1){
System.out.println("Pivot -- Smaller -- Larger");
quickSort(element);
System.out.println("Sorted list : "+sorted);
}
else
{
System.out.println("List is : "+element);
}
}
staticvoidquickSort(ArrayList entire)
{
intsum=0,pivot=0;
ArrayList smaller = newArrayList();
ArrayList higher = newArrayList();
// if n>2 then recurse
if(entire.size()>2)
{
//find pivot element
for(i=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
35/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 35
System.out.println(" "+pivot+" ["+entire.get(0)+"] ["+entire.get(1)+"]");
}
elseif(entire.size()==1)
{sorted.add(entire.get(0));
}
}
}
}
OUTPUT
Enter the number of elements to be sorted:
5
Enter the element 1 :46
Enter the element 2 :
25
Enter the element 3 :
47
Enter the element 4 :
36
Enter the element 5 :
12
Pivot -- Smaller -- Larger
33 [25, 12] [46, 47, 36]
18 [25] [12]
43 [36] [46, 47]
46 [46] [47]
Sorted list : [12, 25, 36, 46, 47]
RESULT Thus the code is written in Java and executed for implementing Merge Sort.
-
5/26/2018 Advanced Data Structures Lab Manual
36/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 36
PRODUCER CONSUMER
ALGORITHM:
Step 1: Start
Step 2: Get choice
Step 3: If choice is to produce
Step 3.1: Get the amount to be produced
Step 3.2: Add the amount to total amount
Step 4: Else if choice is to consume
Step 4.1: Get the amount to be consumed
Step 4.2: Subtract the amount from total amount
Step 5: Else
Step 5.1: Exit
Step 6: If total amount > threshold
Step 7: Exit
Step 8: End Algorithm
JAVA CODE
importjava.util.*;
publicclassProducerConsumer
{
staticintamount=0, currAmt;
staticbooleanflag=true;staticString choice;
publicstaticvoidmain(String[] arg)
{
Scanner s=newScanner(System.in);
System.out.println( "If u want to produce enter p/P");
System.out.println( "If u want to consume enter c/C");
System.out.println( "Others if u want to exit");
while(flag)
{
System.out.println( "Enter your Choice..");
System.out.println( "Amount present in warehouse = "+amount);
choice=s.next();if(choice.equalsIgnoreCase("p"))
-
5/26/2018 Advanced Data Structures Lab Manual
37/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 37
{
System.out.println( "What quantity do u want to produce?");
currAmt=s.nextInt();
amount=amount+currAmt;if(amount>500)flag=false;
}
elseif(choice.equalsIgnoreCase("c"))
{
if(amount==0)
System.out.println( "No production to be consumed..");
else
{
System.out.println( "What quantity do u want to consume?");
currAmt=s.nextInt();
if(currAmt>amount)
System.out.println( "Can't consume more than production !");
else
amount=amount-currAmt;
}
}
else
{
System.out.println( "Wrong choice ! ! ");
flag=false;
}
System.out.println( " ");
}
System.out.println( "The quantity in warehouse is "+amount);
if(amount>500)
System.out.println( "More production and few/no consumption");
}
}
OUTPUT
If u want to produce enter p/P
If u want to consume enter c/C
Others if u want to exit
Enter your Choice..
Amount present in warehouse = 0
p
What quantity do u want to produce?
34
Enter your Choice..
Amount present in warehouse = 34
p
What quantity do u want to produce?
56
Enter your Choice..
Amount present in warehouse = 90
cWhat quantity do u want to consume?
-
5/26/2018 Advanced Data Structures Lab Manual
38/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 38
100
Can't consume more than production !
Enter your Choice..Amount present in warehouse = 90
c
What quantity do u want to consume?
90
Enter your Choice..
Amount present in warehouse = 0
c
No production to be consumed..
Enter your Choice..
Amount present in warehouse = 0
s
Wrong choice ! !
The quantity in warehouse is 0
RESULT Thus the code is written in Java and executed for implementing Producer and consumer
problem.
-
5/26/2018 Advanced Data Structures Lab Manual
39/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 39
CONCURRENT LIST
ALGORITHM:
Step 1: Start
Step 2: Run all the threads simultaneously
Step 3: Lock the list to access it with the thread one and make other threads if present to wait
Step 3.1: Get choice as input
Step 3.2: To add an element list
Step 3.2.1: Get the element and add to the end of the list
Step 3.3: To delete the element
Step 3.3.1: Get the element and remove from the list
Step 3.4: To display the list, print the list
Step 3.5: when choice is wrong, terminate the current thread and unlock the list
Step 4: If a thread is waiting then continue till list is locked
Step 4.1: Check if list is free for each second then
Step 4.2: If list is free then go to Step3
Step 4.3: Else go to Step 4
Step 5: End Algorithm when no thread is waiting
JAVA CODE
List_Thread.java
packageConList;
classRunnableList implementsRunnable
{
Thread runner;
booleanflag=true;
publicRunnableList() {
}
publicvoidrun()
{
System.out.println("Now executing thread----"+ Thread.currentThread());List l = newList();
-
5/26/2018 Advanced Data Structures Lab Manual
40/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 40
l.myList();
}
}
publicclassList_Thread
{
publicstaticvoidmain(String[] args)
{
Thread thread1 = newThread(newRunnableList(), "thread1");
Thread thread2 = newThread(newRunnableList(), "thread2");
//Start the threads
thread1.start();
if(List.choice>3)
{
thread2.start();
}
else
{
System.out.println(" ");
System.out.println("Some thread is accessing the List...");
System.out.println("Now "+Thread.currentThread() +" is waiting......");
create(thread2);
}
}
publicstaticvoidcreate(Thread thread)
{
try{
//delays for one second
Thread.sleep(1000);
} catch(InterruptedException e) {
}
finally
{
if(List.choice>3)
{
thread.start();
}
else
{
create(thread);
}
}
}
}
List.java
packageConList;
importjava.util.*;
publicclassList
{
staticArrayList list1=newArrayList();publicstaticintchoice;
-
5/26/2018 Advanced Data Structures Lab Manual
41/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 41
staticScanner s= newScanner(System.in);
publicvoidmyList()
{
intitem;System.out.println("1. add element");
System.out.println("2. delete element");
System.out.println("3. display list");
System.out.println("Enter choice...");
choice=s.nextInt();
while(choice0)
{
if(choice==1)
{
System.out.println("Enter item to be added..");
item=s.nextInt();
add(item);
}
elseif(choice==2)
{
System.out.println("Enter item to be removed..");
item=s.nextInt();
remove(item);
}
elseif(choice==3)
{
System.out.println("The List is.."+list1);
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
System.out.println("---------Thread Execution completed-----------");
}
staticvoidadd(intitem)
{
list1.add(item);
}
staticvoidremove(intitem)
{
if(!list1.isEmpty())
{
if(list1.contains(item))
list1.remove(find(item));
else
System.out.println("element not found");
}
else
System.out.println("List is empty....");
}
staticintfind(intitem)
{
for(inti=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
42/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 42
returni;
return0;
}
}
OUTPUT
Now executing thread----Thread[thread1,5,main]
1. add element
Some thread is accessing the List...
2. delete elementNow Thread[main,5,main] is waiting......
3. display list
Enter choice...
1
Enter item to be added..
1
Enter choice...
1
Enter item to be added..
2
Enter choice...
1
Enter item to be added..
3
Enter choice...
3
The List is..[1, 2, 3]
Enter choice...
2
Enter item to be removed..
1
Enter choice...
4
---------Thread Execution completed-----------
Now executing thread----Thread[thread2,5,main]
1. add element
2. delete element
3. display list
Enter choice...
3
The List is..[2, 3]
Enter choice...
1
Enter item to be added..
5
Enter choice...
3
The List is..[2, 3, 5]
-
5/26/2018 Advanced Data Structures Lab Manual
43/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 43
Enter choice...
2
Enter item to be removed..
1Enter choice...
5
---------Thread Execution completed-----------
RESULT Thus the code is written in Java and executed for accessing list concurrently.
-
5/26/2018 Advanced Data Structures Lab Manual
44/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 44
CONCURRENT STACK
ALGORITHM:
Step 1: Start
Step 2: Run all the threads simultaneously
Step 3: Lock the stack to access it with the thread one and make other threads if present to wait
Step 3.1: Get choice as input
Step 3.2: To push an element list
Step 3.2.1: Get the element and add to the top of stack
Step 3.3: To pop an element
Step 3.3.1: Remove the element from top of the stack
Step 3.4: To display the stack, print the stack
Step 3.5: when choice is wrong, terminate the current thread and unlock the stack
Step 4: If a thread is waiting then continue till list is locked
Step 4.1: Check if list is free for each second then
Step 4.2: If list is free then go to Step3
Step 4.3: Else go to Step 4
Step 5: End Algorithm when no thread is waiting
JAVA CODE
Stack_Thread.java
packageConStack;
classRunnableStack implementsRunnable
{
Thread runner;
booleanflag=true;
publicRunnableStack() {
}
publicvoidrun()
{
System.out.println("Now executing thread----" +Thread.currentThread());
Stack s = newStack();s.myStack();
-
5/26/2018 Advanced Data Structures Lab Manual
45/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 45
}
}
publicclassStack_Thread{
publicstaticvoidmain(String[] args)
{
Thread thread1 = newThread(newRunnableStack(), "thread1");
Thread thread2 = newThread(newRunnableStack(), "thread2");
//Start the threads
thread1.start();
if(Stack.choice>3)
thread2.start();
else
{
System.out.println(" Some thread is accessing the Stack...");
System.out.println(" Now "+
Thread.currentThread() +" is waiting......");
create(thread2);
}
}
publicstaticvoidcreate(Thread thread)
{
try{
//delays for one second
Thread.sleep(1000);
} catch(InterruptedException e) {
}
finally
{
if(Stack.choice>3)
thread.start();
else
create(thread);
}
}
}
Stack.java
packageConStack;
importjava.util.*;
publicclassStack
{
staticint[] stack1= newint[25];
publicstaticintchoice,top=-1;
staticScanner s= newScanner(System.in);
publicvoidmyStack()
{
intitem;System.out.println("1. Push element");
-
5/26/2018 Advanced Data Structures Lab Manual
46/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 46
System.out.println("2. Pop element");
System.out.println("3. Display Stack");
System.out.println("Enter choice...");
choice=s.nextInt();while(choice0)
{
if(choice==1)
{
if(top>24)
{
System.out.println("Stack is full...");
}
else
{
System.out.println("Enter item to be pushed..");
item=s.nextInt();
push(item);
}
}
elseif(choice==2)
{
if(top==-1)
System.out.println("Stack is empty..");
else
pop();
}
elseif(choice==3)
{
System.out.print("The stack is..");
printStack();
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
System.out.println("-----------Thread Execution completed----------");
}
staticvoidpush(intitem)
{
top++;
stack1[top]=item;
}
staticvoidpop()
{
System.out.println("The element popped is "+stack1[top]);
top--;
}
staticvoidprintStack()
{
if(top==-1)
System.out.print("empty");
else{
-
5/26/2018 Advanced Data Structures Lab Manual
47/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 47
System.out.print("[ ");
for(inti=top;i>-1;i--)
{
System.out.print(stack1[i]+" ");}
System.out.println("]");
}
}
}
OUTPUT
Now executing thread----Thread[thread1,5,main]
Some thread is accessing the Stack...
1. Push element
2. Pop element
3. Display Stack
Enter choice...
Now Thread[main,5,main] is waiting......
1
Enter item to be pushed..
1
Enter choice...
1
Enter item to be pushed..
2
Enter choice...
1
Enter item to be pushed..
3
Enter choice...
3
The stack is..[ 3 2 1 ]
Enter choice...
4
-----------Thread Execution completed----------
Now executing thread----Thread[thread2,5,main]
1. Push element
2. Pop element
3. Display Stack
Enter choice...
3
The stack is..[ 3 2 1 ]
Enter choice...
2
The element popped is 3
Enter choice...
1
Enter item to be pushed..
4
-
5/26/2018 Advanced Data Structures Lab Manual
48/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 48
Enter choice...
3
The stack is..[ 4 2 1 ]
Enter choice...5
-----------Thread Execution completed----------
RESULT Thus the code is written in Java and executed for accessing stack concurrently.
-
5/26/2018 Advanced Data Structures Lab Manual
49/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 49
CONCURRENT QUEUE
ALGORITHM:
Step 1: Start
Step 2: Run all the threads simultaneously
Step 3: Lock the queue to access it with the thread one and make other threads if present to wait
Step 3.1: Get choice as input
Step 3.2: To enqueue an element to the queue
Step 3.2.1: Get the element and add to the end of the queue
Step 3.3: To dequeue an element
Step 3.3.1: remove the element which is in the beginning of the queue
Step 3.4: To display the queue, print the queue
Step 3.5: when choice is wrong, terminate the current thread and unlock the queue
Step 4: If a thread is waiting then continue till list is locked
Step 4.1: Check if list is free for each second then
Step 4.2: If list is free then go to Step3
Step 4.3: Else go to Step 4
Step 5: End Algorithm when no thread is waiting
JAVA CODE
Queue_Thread.java
packageConQueue;
classRunnableQueue implementsRunnable
{
Thread runner;
booleanflag=true;
publicRunnableQueue() {
}
publicvoidrun() {
System.out.println("Now executing thread----"+Thread.currentThread());
Queue l = newQueue();l.myQueue();
-
5/26/2018 Advanced Data Structures Lab Manual
50/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 50
}
}
publicclassQueue_Thread{
publicstaticvoidmain(String[] args)
{
Thread thread1 = newThread(newRunnableQueue(), "thread1");
Thread thread2 = newThread(newRunnableQueue(), "thread2");
//Start the threads
thread1.start();
if(Queue.choice>3)
thread2.start();
else
{
System.out.println(" ");
System.out.println("Some thread is accessing the Queue...");
System.out.println("Now "+Thread.currentThread() +" is waiting......");
create(thread2);
}
}
publicstaticvoidcreate(Thread thread)
{
try{
//delays for one second
Thread.sleep(1000);
} catch(InterruptedException e) {
}
finally
{
if(Queue.choice>3)
thread.start();
else
create(thread);
}
}
}
Queue.java
packageConQueue;
importjava.util.*;
publicclassQueue
{
staticint[] queue1= newint[25];
publicstaticintchoice,rear=0,front=0;
staticScanner s= newScanner(System.in);
publicvoidmyQueue()
{
intitem;
System.out.println("1. Push element");
System.out.println("2. Pop element");System.out.println("3. Display Stack");
-
5/26/2018 Advanced Data Structures Lab Manual
51/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 51
System.out.println("Enter choice...");
choice=s.nextInt();
while(choice0)
{if(choice==1)
{
if(rear>24)
System.out.println("Modifications on 25 elements processed..");
else
{
System.out.println("Enter item to be added..");
item=s.nextInt();
enqueue(item);
}
}
elseif(choice==2)
{
if(rear==front)
System.out.println("Queue is empty");
else
dequeue();
}
elseif(choice==3)
{
System.out.println("The queue is..");
printQueue();
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
System.out.println("-----------Thread Execution completed----------");
}
staticvoidenqueue(intitem)
{
queue1[rear]=item;
rear++;
}
staticvoiddequeue()
{
System.out.println("The element dequeued is "+queue1[front]);
front++;
}
staticvoidprintQueue()
{
if(rear==front)
System.out.print("empty");
else
{
System.out.print("[ ");
for(inti=front;i
-
5/26/2018 Advanced Data Structures Lab Manual
52/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 52
}
}
}
OUTPUT
Now executing thread----Thread[thread1,5,main]
Some thread is accessing the Queue...
Now Thread[main,5,main] is waiting......1. Push element
2. Pop element
3. Display Stack
Enter choice...
1
Enter item to be added..
1
Enter choice...
1
Enter item to be added..
2
Enter choice...
1
Enter item to be added..
3
Enter choice...
3
The queue is..
[ 1 2 3 ]
Enter choice...
2
The element dequeued is 1
Enter choice...
4
---------Thread Execution completed-----------
Now executing thread----Thread[thread2,5,main]
1. Push element
2. Pop element
3. Display Stack
Enter choice...
3
The queue is..
[ 2 3 ]
Enter choice...
1
Enter item to be added..
4Enter choice...
-
5/26/2018 Advanced Data Structures Lab Manual
53/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 53
3
The queue is..
[ 2 3 4 ]
Enter choice...5
---------Thread Execution completed-----------
RESULT Thus the code is written in Java and executed for accessing queue concurrently.
-
5/26/2018 Advanced Data Structures Lab Manual
54/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 54
DYNAMIC PROGRAMMING
ALGORITHM:
Step 1: Start
Step 2: Get an input graph G, source node S with cost of each edge c
Step 3: Assign path of each node as NIL
Step 4: Assign distance of each node except S as INFINITY
Step 5: Assign distance of S as 0
Step 6: Add S to notHandled
Step 7: Sort the nodes based on the distance
Step 8: Choose the node U, with minimum distance
Step 8.1: Add all children of U to notHandled
Step 8.2: For all the child nodes of U
Step 8.2.1: Assign path as node U
Step 8.2.2: If distance of current node greater than d(U) + c
Step 8.2.2.1: Assign distance as d(U) + c
Step 8.3: Move U from notHandled to Handled
Step 9: Go to step 7 if notHandled is not empty
Step 10: Return all the nodes and their cost from the source node S
Step 11: End Algorithm
JAVA CODE
importjava.util.*;
publicclassDynamicPrgm
{
staticArrayList start= new ArrayList();
staticArrayList end= newArrayList();
staticArrayList cost= newArrayList();
staticArrayList nodes= newArrayList();
staticint[] length= newint[20];
staticArrayList handled= newArrayList();
staticArrayList notHandled= newArrayList();
-
5/26/2018 Advanced Data Structures Lab Manual
55/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 55
staticintedgeCount;
publicstaticvoidmain(String[] arg){
intnode;
inti;
Scanner s= newScanner(System.in);
// Get start node, end node, cost of each edge from user
System.out.println( "NOTE: The first node will be taken as source");
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i
-
5/26/2018 Advanced Data Structures Lab Manual
56/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 56
3
5
72
6
4
p
q r
ts
{
intcost=1000;
intnode=0;
for(intk=0; klength[k] && !(handled.contains(nodes.get(k))))
{
cost=length[k];
node=k;
}
returnnode;
}
staticvoidfindChild(intnode)
{
for(intj=0;j
-
5/26/2018 Advanced Data Structures Lab Manual
57/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 57
OUTPUT
NOTE: The first node will be taken as source
Enter the number of edges:6
Enter the FROM node:
p
Enter the TO node:
q
Enter the cost of edge:
5
Enter the FROM node:
p
Enter the TO node:
r
Enter the cost of edge:4
Enter the FROM node:
q
Enter the TO node:
s
Enter the cost of edge:
7
Enter the FROM node:
q
Enter the TO node:
t
Enter the cost of edge:
3
Enter the FROM node:
r
Enter the TO node:
s
Enter the cost of edge:
2
Enter the FROM node:
r
Enter the TO node:
t
Enter the cost of edge:
6
NODE LENGTH
p - 0
q - 5
r - 4
s - 6
t - 8
RESULT Thus the code is written in Java and executed for performing dynamic programming in
calculating the cost of each node from source node.
-
5/26/2018 Advanced Data Structures Lab Manual
58/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 58
RANDOMIZED ALGORITHM
ALGORITHM:
Step 1: Start
Step 2: Get the choice from user either Combination or Permutation
Step 3: If choice is Combination
Step 3.1: Get n and r
Step 3.2: Calculate n! / [(n-r)! r!]
Step 3.3: Return result
Step 4: If choice is Permutation
Step 4.1: Get n and r
Step 4.2: Calculate n! / (n-r)!
Step 4.3: Return result
Step 5: End Algorithm
JAVA CODE
packageDynamicProgram;
importjava.util.*;
publicclassRandomizedAlgm
{
staticbooleanflag=true;
staticString choice;
staticintn;
staticintr;publicstaticvoidmain(String[] arg)
{
Scanner s=newScanner(System.in);
System.out.println( "If u want to find Permutations p/P");
System.out.println( "If u want to find Combinations c/C");
System.out.println( "Others if u want to exit");
while(flag)
{
System.out.println( " ");
System.out.println( "Enter your choice..");
choice=s.next();
if(choice.equals("p")){
System.out.println( "TO CALCULATE nPr :");
-
5/26/2018 Advanced Data Structures Lab Manual
59/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 59
System.out.println( "Enter the total no. of items, n :");
n=s.nextInt();
System.out.println( "Enter the no. of items in one sample, r :");
r=s.nextInt();if(n>=r)
findP(n,r);
else
System.out.println( "r should be lesser than or equal to n");
}
elseif(choice.equals("c"))
{
System.out.println( "TO CALCULATE nCr :");
System.out.println( "Enter the total no. of items, n :");
n=s.nextInt();
System.out.println( "Enter the no. of items in one sample, r :");
r=s.nextInt();
if(n>=r)
findC(n,r);
else
System.out.println( "r should be lesser than or equal to n");
}
else
{
System.out.println( "Wrong Choice...");
flag=false;
}
}
}
publicstaticvoidfindP(intn, intr)
{
// npr = n!/(n-r)!
intresult=fact(n)/fact(n-r);
System.out.println( n+" P "+r+" = "+result);
}
publicstaticvoidfindC(intn, intr)
{
// ncr = n!/(n-r)!r!
intresult=fact(n)/(fact(n-r)*fact(r));
System.out.println( n+" C "+r+" = "+result);
}
publicstaticintfact(intn)
{
intfact=1;
for(inti=1;i
-
5/26/2018 Advanced Data Structures Lab Manual
60/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 60
OUTPUT
If u want to find Permutations p/P
If u want to find Combinations c/C
Others if u want to exit
Enter your choice..
p
TO CALCULATE nPr :
Enter the total no. of items, n :
10
Enter the no. of items in one sample, r :
5
10 P 5 = 30240
Enter your choice..
c
TO CALCULATE nCr :
Enter the total no. of items, n :
7
Enter the no. of items in one sample, r :
4
7 C 4 = 35
Enter your choice..
h
Wrong Choice...
RESULT Thus the code is written in Java and executed for a randomized algorithm to calculate
combination and permutation.
-
5/26/2018 Advanced Data Structures Lab Manual
61/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 61
DINING PHILOSOPHERS PROBLEM
Algorithm:
Step 1: Start
Step 2: Assume all philosophers are reading
Step 3: Input a philosopher and his action
Step 3.1: If action is to eat
Step 3.1.1: Check if both chopsticks are available for the philosopher then
Step 3.1.1.1: Lock the chopsticks
Step 3.1.2: Else keep the philosopher waiting
Step 3.2: if action is to read
Step 3.2.1: Check if the chopsticks are locked then
Step 3.2.1.1: Unlock the chopsticks
Step 3.3: Else Exit
Step 4: End Algorithm
JAVA CODE
packageDynamicProgram;
importjava.util.*;
publicclassPhilosopherPrblm
{
staticScanner s=newScanner(System.in);
staticint[] reader={0,0,0,0,0};staticint[] chopStick={0,0,0,0,0};
staticintphilo;
staticString choice;
staticbooleanflag=true;
publicstaticvoidmain(String[] arg)
{
System.out.println( "NOTE: Initally all philosophers are reading...");
while(flag)
{
System.out.println( "Select a philosopher..");
philo=s.nextInt();if(philo0)
{
-
5/26/2018 Advanced Data Structures Lab Manual
62/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 62
System.out.println( "Does he want to eat(e/E) / read(r/R)?");
choice=s.next();
if(choice.equalsIgnoreCase("e"))
{switch(philo)
{
case1:
lock(0,1);
break;
case2:
lock(1,2);
break;
case3:
lock(2,3);
break;
case4:
lock(3,4);
break;
case5:
lock(4,0);
break;
}
}
elseif(choice.equalsIgnoreCase("r"))
{
switch(philo)
{
case1:
unlock(0,1);
break;
case2:
unlock(1,2);
break;
case3:
unlock(2,3);
break;
case4:
unlock(3,4);
break;
case5:
unlock(4,0);
break;
}
}
else
{
System.out.println( "Wrong choice.. The philosopher can either
read or eat..");
flag=false;
}
job();
System.out.println( ".........................................");
}else
-
5/26/2018 Advanced Data Structures Lab Manual
63/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 63
{
System.out.println( "There are only 5 philosophers on the table..");
flag=false;
}}
}
staticvoidlock(intm, intn)
{
if(chopStick[m]==0 && chopStick[n]==0)
{
chopStick[m]=1;
chopStick[n]=1;
reader[m]=1;
}
else
{
System.out.println( "Wait till philosopher's neighbour finishes eating..");
System.out.println( " ");
}
}
staticvoidunlock(intm,intn)
{
chopStick[m]=0;
chopStick[n]=0;
reader[m]=0;
}
staticvoidjob()
{
for(inti=0;i
-
5/26/2018 Advanced Data Structures Lab Manual
64/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 64
1
2
3
5
4
1
2
3
4
5
PICTURIZATION
OUTPUT
NOTE: Initally all philosophers are reading...
Select a philosopher..
2
Does he want to eat(e/E) / read(r/R)?
e
Philosopher 1 is reading
Philosopher 2 is eating
Philosopher 3 is reading
Philosopher 4 is reading
Philosopher 5 is reading
.........................................
Select a philosopher..
4
Does he want to eat(e/E) / read(r/R)?
e
Philosopher 1 is reading
Philosopher 2 is eating
Philosopher 3 is reading
Philosopher 4 is eating
Philosopher 5 is reading
.........................................
Select a philosopher..
-
5/26/2018 Advanced Data Structures Lab Manual
65/65
UNITED INSTITUTE OF TECHNOLOGYPeriyanaickenpalayam, Coimbatore- 641 020.Department of Computer Science and Engineering
Advanced Data Structures Laboratory
Antonita Shilpa . J UIT ( FMCS02 ) 65
5
Does he want to eat(e/E) / read(r/R)?
e
Wait till philosopher's neighbour finishes eating..
Philosopher 1 is reading
Philosopher 2 is eating
Philosopher 3 is reading
Philosopher 4 is eating
Philosopher 5 is reading
.........................................
Select a philosopher..
6
There are only 5 philosophers on the table..
RESULT Thus the code is written in Java and executed for the implementation of diningphilosophers problem.