You are on page 1of 65

UNITED INSTITUTE OF TECHNOLOGY

Periyanaickenpalayam, Coimbatore- 641 020.

Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 1

INDEX
S. No. PROGRAM Page No.
1 Graph Search 2
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
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 <Handled, notHandled>
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 <Handled, notHandled>
Step 7: End the Algorithm

JAVA CODE
import java.util.*;
public class GraphSearch
{
public static void main(String[] arg)
{
ArrayList<String> start = new ArrayList<String>();
ArrayList<String> end = new ArrayList<String>();
ArrayList<String> nodes = new ArrayList<String>();
ArrayList<String> notHandled = new ArrayList<String>();
ArrayList<String> handled = new ArrayList<String>();
int edgeCount, i, hSize, nSize;
String node;
Scanner s= new Scanner(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<edgeCount;i++)
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 3

// get FROM node and TO node for each edge
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
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:");
}
if(!(nodes.contains(start.get(i))))
if(!(nodes.contains(end.get(i))))
}
nSize=nodes.size();
System.out.println( "Enter the node to be found:");
node=s.next();
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<nSize)
{

// for the rear node in handled queue, add its children in notHandled queue
for(i=0; i<start.size(); i++)
if(handled.get(hSize-1).equalsIgnoreCase(start.get(i)))
if(!(notHandled.contains(end.get(i))) &&
!(handled.contains(end.get(i))))

//remove the front node from notHandled and add it to the handled queue
if(!(handled.contains(notHandled.get(0))) &&
!(notHandled.get(0).equalsIgnoreCase(node)))
if(node.equalsIgnoreCase(notHandled.get(0)))
hSize=nSize;
else
hSize=handled.size();
nodes.remove(notHandled.get(0));
notHandled.remove(0);
}
System.out.println(" ");
System.out.println("Nodes Handled = "+handled);
System.out.println("Node "+ node +" Found !!! ");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 4

a
b c
e d
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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 5

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 <Handled, notHandled>
Step 7: End the Algorithm

JAVA CODE
import java.util.*;
{
public static void main(String[] arg)
{
ArrayList<String> start = new ArrayList<String>();
ArrayList<String> end = new ArrayList<String>();
ArrayList<String> nodes = new ArrayList<String>();
ArrayList<String> notHandled = new ArrayList<String>();
ArrayList<String> handled = new ArrayList<String>();
int edgeCount, i, hSize;

Scanner s= new Scanner(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<edgeCount;i++)
{
// get FROM node and TO node for each edge
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
while(start.get(i).equalsIgnoreCase(end.get(i)))
{
end.remove(i);
System.out.println( "Error: SAME AS START NODE");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 6

System.out.println( "Enter the TO node:");
}
if(!(nodes.contains(start.get(i))))
if(!(nodes.contains(end.get(i))))
}
System.out.println("Handled Not Handled");
System.out.println("[] "+ notHandled);
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; i<edgeCount; i++)
if(handled.get(hSize-1).equalsIgnoreCase(start.get(i)))
if(!(notHandled.contains(end.get(i))) &&
!(handled.contains(end.get(i))))
System.out.println(handled +" "+ notHandled);
//remove the front node from notHandled and add it to the handled queue
if(notHandled.size()>0)
if(!(handled.contains(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
e c
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 <Handled, notHandled>
Step 7: End the Algorithm

JAVA CODE
import java.util.*;
public class DepthFirstSearch
{
static ArrayList<String> start = new ArrayList<String>();
static ArrayList<String> end = new ArrayList<String>();
static String[] stack = new String[10];
static ArrayList<String> handled = new ArrayList<String>();
static ArrayList<String> notHandled = new ArrayList<String>();
static ArrayList<String> child = new ArrayList<String>();
static int edgeCount,top=-1;
public static void main(String[] arg)
{
Scanner s= new Scanner(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(int i=0; i<edgeCount;i++)
{
// get FROM node and TO node for each edge
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
while(start.get(i).equalsIgnoreCase(end.get(i)))
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 9

{
end.remove(i);
System.out.println( "Error: SAME AS START NODE");
System.out.println( "Enter the TO node:");
}
}
System.out.println("Handled STACK");
check(start.get(0));
printStack();
System.out.println("All Nodes are traversed in DFS");
}

static void cSort()
{
//since the child nodes are inserted in reverse order, reverse it
for(int q=child.size();q>0;q--)
push(child.get(q-1));
}

static void push(String node)
{
// add node to stack and notHandled
if(!(notHandled.contains(node)) && !(handled.contains(node)))
{
top++;
stack[top]=node;
}
}

static void pop()
{
//move the top node from notHandled to handled
if(top>-1)
{
notHandled.remove(stack[top]);
top--;
}
}

static void check(String node)
{
// insert the parent node in notHandled node
push(node);
printStack();
int flag=1;
for(int q=0; q<start.size();q++)
if(start.get(q).equalsIgnoreCase(node))
{
//the child nodes for top of stack if any
if(flag==1)
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 10

a
b
c
e d
pop();
flag=0;
}
//since they are not arranged, use child array ad temp array
}
//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]);
}

static void printStack()
{
//print the notHandled node in reverse order, since its stack
ArrayList<String> sort = new ArrayList<String>();
for(int q=notHandled.size();q>0;q--)
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
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 <u, v>
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 <u, v>
Step 8.2.2.1: Assign distance as d(U) + c <u, v>
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
import java.util.*;

public class ShortestPath_Dijikstra
{
static ArrayList<String> start = new ArrayList<String>();
static ArrayList<String> end = new ArrayList<String>();
static ArrayList<Integer> cost = new ArrayList<Integer>();
static ArrayList<String> nodes = new ArrayList<String>();
static String[] path = new String[20];
static int[] length = new int[20];
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 13

static ArrayList<String> handled = new ArrayList<String>();
static ArrayList<String> notHandled = new ArrayList<String>();
static int edgeCount;

public static void main(String[] arg)
{
int node, i;
Scanner s= new Scanner(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<edgeCount;i++)
{
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
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:");
}
System.out.println( "Enter the cost of edge:");
if(!(nodes.contains(start.get(i))))
if(!(nodes.contains(end.get(i))))
}
System.out.println("No. of nodes="+nodes.size());
//Initialising table values
for(i=0;i<nodes.size();i++)
if(i==0)
{
// for ROOT/SOURCE node
length[i]=0;
path[i]="NIL";
}
else
// other nodes in the graph
length[i]=1000;
System.out.println("NODE KNOWN LENGTH PATH");
print();
for(i=0;i<nodes.size();i++)
{
node=findMinimum();
findChild(node);
System.out.println("NODE KNOWN LENGTH PATH");
print();
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 14

System.out.println(" ");
notHandled.clear();
}
System.out.println("NODE KNOWN LENGTH PATH");
print();
System.out.println("");
}
static int findMinimum()
{
int cost=1000;
int node=0;
for(int k=0; k<nodes.size(); k++)
if(cost>length[k] && !(handled.contains(nodes.get(k))))
{
cost=length[k];
node=k;
}
return node;
}

static void findChild(int node)
{
for(int j=0;j<edgeCount;j++)
if(nodes.get(node).equalsIgnoreCase(start.get(j)))
if(!(handled.contains(end.get(j))))
{
for(int k=0; k<nodes.size();k++)
{
if(end.get(j).equalsIgnoreCase(nodes.get(k)))
{
if(length[k]>(cost.get(j)+length[node]))
{
length[k]= cost.get(j)+length[node];
path[k]=start.get(j);
}
}
}

}
}

static void print()
{
for(int k=0;k<nodes.size();k++)
System.out.println(nodes.get(k)+" "+known(k)+" "+length[k]+" "+path[k]);
}

static int known(int index)
{
if(handled.contains(nodes.get(index)))
return 1;
else
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 15

3
5
7
2
6
4
p
q r
t s
return 0;
}

}

GRAPH

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:
5
Enter the FROM node:
q
Enter the TO node:
t
Enter the cost of edge:
3
Enter the FROM node:
r
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 16

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
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 r
t 0 8 q
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 17

NODE KNOWN LENGTH PATH
p 1 0 NIL
q 1 5 p
r 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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package Graph;
import java.util.*;

public class TopologicalSort
{
static ArrayList<String> start = new ArrayList<String>();
static ArrayList<String> end = new ArrayList<String>();
static ArrayList<String> nodes = new ArrayList<String>();
static int[] indegree = new int[20];
static ArrayList<String> handled = new ArrayList<String>();
static int edgeCount, i, nodeSize, k, j, count, pNodeSize;
static boolean flag=true;

public static void main(String[] arg)
{
int min;
Scanner s= new Scanner(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<edgeCount;i++)
{
// get FROM node and TO node for each edge
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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:");
}
if(!(nodes.contains(start.get(i))))
if(!(nodes.contains(end.get(i))))
}
nodeSize=nodes.size();
for(k=0;k<nodeSize;k++)
{
if(flag)
{
pNodeSize=nodes.size();
inDegree();
if(check())
{
min=minDegree();
if(!(handled.contains(nodes.get(min))))
{
}
nodes.remove(min);
System.out.println(" ");
System.out.println( "Sorted queue "+handled);
System.out.println("-----------------------------------");
System.out.println(" ");
}
else
{
System.out.println("The graph is cyclic and hence cant be sorted");
flag=false;
}
}
}
}

static void inDegree()
{
for(i=0;i<pNodeSize;i++)
{
count=0;
for(j=0;j<edgeCount;j++)
{
if(nodes.get(i).equalsIgnoreCase(end.get(j)) &&
!(handled.contains(start.get(j))))
count++;
}
indegree[i]=count;
System.out.println( "indegree of "+nodes.get(i)+" is "+count);
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 20

a
e
b
c
d
}
}

static int minDegree()
{
int min=indegree[0],index=0;
for(i=0;i<pNodeSize;i++)
{
if(min>indegree[i])
{
min=indegree[i];
index=i;
}
}
return index;
}

static boolean check()
{
for(i=0;i<pNodeSize;i++)
{
if(indegree[i]==0)
return true;
}
return false;
}
}
GRAPH

OUTPUT
NOTE: The first node will be taken as source
Enter the number of edges:
5
Enter the FROM node:
a
Enter the TO node:
b
Enter the FROM node:
b
Enter the TO node:
c
Enter the FROM node:
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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_capacity min_cut
Step 5.3.2: Assign the result to flow_capacity of the edge
Step 5.3.3: Calculate and assign netFlow = netFlow min_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
import java.util.*;
public class HillClimbing
{
static ArrayList<String> start = new ArrayList<String>();
static ArrayList<String> end = new ArrayList<String>();
static ArrayList<String> nodes = new ArrayList<String>();
static ArrayList<Integer> capacity = new ArrayList<Integer>();
static ArrayList<String> stack = new ArrayList<String>();
static int []flow = new int[20];
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 23

static String dest,source;
static int edgeCount,i,j,netFlow,flag=0,top,netCapacity=0,capa;
public static void main(String[] arg)
{
Scanner s= new Scanner(System.in);
System.out.println( "Enter the number of edges:");
edgeCount=s.nextInt();
for(i=0; i<edgeCount;i++)
{
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
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:");
}
System.out.println( "Enter the capacity of the edge:");
capa=s.nextInt();
flow[i]=capa;
}
capa=0;
System.out.println( "Enter the Source Node ");
source=s.next();
System.out.println( "Enter the Destination Node ");
dest=s.next();
System.out.println( "Enter the Flow/Rate of the network ");
netFlow=s.nextInt();
findPath(source);
stack.clear();
flag=1;
findPath(source);
print();
check();
}

static void findPath(String node)
{
ArrayList<String> queue = new ArrayList<String>();
for(i=0;i<edgeCount;i++)
if(start.get(i).equalsIgnoreCase(node))
if(!stack.contains(end.get(i)))
{
if(end.get(i).equalsIgnoreCase(dest))
{
pathCapacity(stack);
stack.remove(stack.size()-1);
}
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 24

else
}
while(!queue.isEmpty())
{
findPath(queue.get(0));
stack.remove(stack.size()-1);
queue.remove(0);
}
}

static void pathCapacity(ArrayList<String> path)
{
int min=1000;
ArrayList<Integer> edges = new ArrayList<Integer>();
for(j=0;j<(path.size()-1);j++)
{
for(int k=0;k<edgeCount;k++)
{
if(start.get(k).equalsIgnoreCase(path.get(j)) &&
end.get(k).equalsIgnoreCase(path.get(j+1)))
{
if(min>flow[k])
min=flow[k];
}
}
}

if(flag==0)
netCapacity=netCapacity+min;
else
{
if(netFlow>min)
{
netFlow=netFlow-min;
capa=capa+min;
System.out.println("Flow along the PATH "+stack+" with FLOW "+min);
for(int q=0;q<edges.size();q++)
{
int index=edges .get(q);
flow[index]=flow[index]-min;
}
}
else
{
capa=capa+netFlow;
System.out.println("Flow along the PATH "+stack+" with FLOW " +netFlow);
for(int q=0;q<edges.size();q++)
{
int index=edges .get(q);
flow[index]=flow[index]-netFlow;
}
netFlow=0;
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 25

8
16
7
12 42
17
15
10
b
13
a
e
c
d
t
s
}

}
}

static void check()
{
if(netFlow>0)
{
System.out.println("The Maximum flow of the network is "+capa);
System.out.println("The requested flow cant be accomodated.."+netFlow);
}
}

static void print()
{
System.out.println(" ");
for(j=0;j<edgeCount;j++)
{
System.out.println("Flow across the edge "+start.get(j)+" "+end.get(j)+" is : "+
(capacity.get(j)-flow[j])+"/"+capacity.get(j));
}
}
}

GRAPH

OUTPUT
Enter the number of edges:
9
Enter the FROM node:
s
Enter the TO node:
a
Enter the capacity of the edge:
10
Enter the FROM node:
s
Enter the TO node:
d
Enter the capacity of the edge:
13
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 26

Enter the FROM node:
s
Enter the TO node:
c
Enter 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
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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/7
Flow 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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
import java.util.*;

public class MergeSort
{
static int n,i;
static ArrayList<Integer> element = new ArrayList<Integer>();
static ArrayList<Integer> sorted = new ArrayList<Integer>();
public static void main(String[] arg)
{
Scanner s= new Scanner(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<n;i++)
{
System.out.println( "Enter the element "+(i+1)+" :");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 29

}
splitting(element);
System.out.println(" ");
System.out.println("Sorted List of given List is : "+sorted);
}

static void splitting(ArrayList<Integer> entire)
{
int size=entire.size()/2;
ArrayList<Integer> right = new ArrayList<Integer>();
ArrayList<Integer> left = new ArrayList<Integer>();
// if n>2 then recurse
if(entire.size()>2)
{
//divide into 2
for(i=0;i<entire.size();i++)
if(i<size)
else
splitting(right);
splitting(left);
merging(right,left);
}
if(entire.size()==2)
{
merging(right,left);
}
}

static void merging(ArrayList<Integer> right,ArrayList<Integer> left)
{
ArrayList<Integer> merged = new ArrayList<Integer>();
int rs=right.size(),ls=left.size(),x=0,y=0,z=0,ss=sorted.size();
int sum=rs+ls+ss;
i=0;
System.out.println("Left : "+left);
System.out.println("Previously Sorted : "+sorted);
System.out.println("Right : "+right);

while(i<(sum))
{
if(rs>x && ls==y && ss==z)//only right
{
if(!merged.contains(right.get(x)))
x++;
i++;
}
else if(ls>y && rs==x && ss==z)//only left
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 30

if(!merged.contains(left.get(y)))
y++;
i++;
}
else if(ls==y && rs==x && ss>z)//only sort
{
if(!merged.contains(sorted.get(z)))
z++;
i++;
}
else if(ls>y && rs==x && ss>z)//left n sort
{
if(left.get(y)<sorted.get(z))
{
if(!merged.contains(left.get(y)))
y++;
i++;
}
else
{
if(!merged.contains(sorted.get(z)))
z++;
i++;
}
}
else if(ls==y && rs>x && ss>z)//right n sort
{
if(right.get(x)<sorted.get(z))
{
if(!merged.contains(right.get(x)))
x++;
i++;
}
else
{
if(!merged.contains(sorted.get(z)))
z++;
i++;
}
}
else if(ls>y && rs>x && ss==z)//right n left
{
if(right.get(x)<left.get(y))
{
if(!merged.contains(right.get(x)))
x++;
i++;
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 31

}
else
{
if(!merged.contains(left.get(y)))
y++;
i++;
}
}
else if(ls>y && rs>x && ss>z)//right, sort n left
{
if(left.get(y)<right.get(x))
{
if(left.get(y)<sorted.get(z))
{
if(!merged.contains(left.get(y)))
y++;
i++;
}
else
{
if(!merged.contains(sorted.get(z)))
z++;
i++;
}

}
else
{
if(right.get(x)<sorted.get(z))
{
if(!merged.contains(right.get(x)))
y++;
i++;
}
else
{
if(!merged.contains(sorted.get(z)))
z++;
i++;
}
}
}
else
{
System.out.println("Error.........");
}
}
System.out.println("-----------------------------");
System.out.println("Locally Merged Sorted List : "+merged);
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 :
3
Enter 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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
import java.util.*;
public class QuickSort
{
static int n,i;
static ArrayList<Integer> element = new ArrayList<Integer>();
static ArrayList<Integer> sorted = new ArrayList<Integer>();
public static void main(String[] arg)
{
Scanner s= new Scanner(System.in);
//get elements
System.out.println( "Enter the number of elements to be sorted:");
n=s.nextInt();
for(i=0;i<n;i++)
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 34

System.out.println( "Enter the element "+(i+1)+" :");
}
if(n>1)
{
System.out.println("Pivot -- Smaller -- Larger");
quickSort(element);
System.out.println("Sorted list : "+sorted);
}
else
{
System.out.println("List is : "+element);
}
}

static void quickSort(ArrayList<Integer> entire)
{
int sum=0,pivot=0;
ArrayList<Integer> smaller = new ArrayList<Integer>();
ArrayList<Integer> higher = new ArrayList<Integer>();
// if n>2 then recurse
if(entire.size()>2)
{
//find pivot element
for(i=0;i<entire.size();i++)
sum=sum+entire.get(i);
pivot=sum/entire.size();
//divide into 2
for(i=0;i<entire.size();i++)
if(entire.get(i)>pivot)
else
System.out.println(" "+pivot+" "+smaller+" "+higher);
quickSort(smaller);
quickSort(higher);

}
//else sort and merge
else
{
if(entire.size()==2)
{
pivot=(entire.get(0)+entire.get(1))/2;
if(entire.get(0)<entire.get(1))
{
}
else
{
}
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 35

System.out.println(" "+pivot+" ["+entire.get(0)+"] ["+entire.get(1)+"]");
}
else if(entire.size()==1)
{
}

}
}
}
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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
import java.util.*;
public class ProducerConsumer
{
static int amount=0, currAmt;
static boolean flag=true;
static String choice;
public static void main(String[] arg)
{
Scanner s=new Scanner(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( "Amount present in warehouse = "+amount);
choice=s.next();
if(choice.equalsIgnoreCase("p"))
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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;
}
else if(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
Amount present in warehouse = 0
p
What quantity do u want to produce?
34

Amount present in warehouse = 34
p
What quantity do u want to produce?
56

Amount present in warehouse = 90
c
What quantity do u want to consume?
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 38

100
Can't consume more than production !

Amount present in warehouse = 90
c

What quantity do u want to consume?
90

Amount present in warehouse = 0
c
No production to be consumed..

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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package ConList;
class RunnableList implements Runnable
{
boolean flag=true;
public RunnableList() {

}
public void run()
{
List l = new List();
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 40

l.myList();
}
}

{
public static void main(String[] args)
{
if(List.choice>3)
{
}
else
{
System.out.println(" ");
System.out.println("Some thread is accessing the List...");
}
}

{
try {
//delays for one second
} catch (InterruptedException e) {
}
finally
{
if(List.choice>3)
{
}
else
{
}
}
}
}
List.java
package ConList;
import java.util.*;
public class List
{
static ArrayList<Integer> list1=new ArrayList<Integer>();
public static int choice;
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 41

static Scanner s = new Scanner(System.in);
public void myList()
{
int item;
System.out.println("2. delete element");
System.out.println("3. display list");
System.out.println("Enter choice...");
choice=s.nextInt();
while(choice<4 && choice>0)
{
if(choice==1)
{
item=s.nextInt();
}
else if(choice==2)
{
System.out.println("Enter item to be removed..");
item=s.nextInt();
remove(item);
}
else if(choice==3)
{
System.out.println("The List is.."+list1);
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
}
{
}

static void remove(int item)
{
if(!list1.isEmpty())
{
if(list1.contains(item))
list1.remove(find(item));
else
}
else
System.out.println("List is empty....");
}

static int find(int item)
{
for(int i=0;i<list1.size();i++)
if(list1.get(i)==item)
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 42

return i;
return 0;
}
}

OUTPUT

Some thread is accessing the List...
2. delete element
3. display list
Enter choice...
1
1
Enter choice...
1
2
Enter choice...
1
3
Enter choice...
3
The List is..[1, 2, 3]
Enter choice...
2
Enter item to be removed..
1
Enter choice...
4
2. delete element
3. display list
Enter choice...
3
The List is..[2, 3]
Enter choice...
1
5
Enter choice...
3
The List is..[2, 3, 5]
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 43

Enter choice...
2
Enter item to be removed..
1
Enter choice...
5

RESULT Thus the code is written in Java and executed for accessing list concurrently.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package ConStack;
class RunnableStack implements Runnable
{
boolean flag=true;
public RunnableStack() {
}
public void run()
{
Stack s = new Stack();
s.myStack();
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 45

}
}

{
public static void main(String[] args)
{
if(Stack.choice>3)
else
{
System.out.println("<-------------> Some thread is accessing the Stack...");
System.out.println("<-------------> Now "+
}
}

{
try {
//delays for one second
} catch (InterruptedException e) {
}
finally
{
if(Stack.choice>3)
else
}
}
}
Stack.java
package ConStack;
import java.util.*;

public class Stack
{
static int[] stack1 = new int[25];
public static int choice,top=-1;
static Scanner s = new Scanner(System.in);

public void myStack()
{
int item;
System.out.println("1. Push element");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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(choice<4 && choice>0)
{
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);
}
}
else if(choice==2)
{
if(top==-1)
System.out.println("Stack is empty..");
else
pop();
}
else if(choice==3)
{
System.out.print("The stack is..");
printStack();
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
}
static void push(int item)
{
top++;
stack1[top]=item;
}

static void pop()
{
System.out.println("The element popped is "+stack1[top]);
top--;
}

static void printStack()
{
if(top==-1)
System.out.print("empty");
else
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 47

System.out.print("[ ");
for(int i=top;i>-1;i--)
{
System.out.print(stack1[i]+" ");
}
System.out.println("]");
}
}
}

OUTPUT
<-------------> Some thread is accessing the Stack...
1. Push element
2. Pop element
3. Display Stack
Enter choice...
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
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
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 48

Enter choice...
3
The stack is..[ 4 2 1 ]
Enter choice...
5

RESULT Thus the code is written in Java and executed for accessing stack concurrently.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package ConQueue;
class RunnableQueue implements Runnable
{
boolean flag=true;
public RunnableQueue() {

}
public void run() {
Queue l = new Queue();
l.myQueue();
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 50

}
}

{
public static void main(String[] args)
{
if(Queue.choice>3)
else
{
System.out.println(" ");
System.out.println("Some thread is accessing the Queue...");
}
}

{
try {
//delays for one second
} catch (InterruptedException e) {
}
finally
{
if(Queue.choice>3)
else
}
}
}
Queue.java
package ConQueue;
import java.util.*;
public class Queue
{
static int[] queue1 = new int[25];
public static int choice,rear=0,front=0;
static Scanner s = new Scanner(System.in);
public void myQueue()
{
int item;
System.out.println("1. Push element");
System.out.println("2. Pop element");
System.out.println("3. Display Stack");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 51

System.out.println("Enter choice...");
choice=s.nextInt();
while(choice<4 && choice>0)
{
if(choice==1)
{
if(rear>24)
System.out.println("Modifications on 25 elements processed..");
else
{
item=s.nextInt();
enqueue(item);
}
}
else if(choice==2)
{
if(rear==front)
System.out.println("Queue is empty");
else
dequeue();
}
else if(choice==3)
{
System.out.println("The queue is..");
printQueue();
}
System.out.println("Enter choice...");
choice=s.nextInt();
}
}
static void enqueue(int item)
{
queue1[rear]=item;
rear++;
}

static void dequeue()
{
System.out.println("The element dequeued is "+queue1[front]);
front++;
}

static void printQueue()
{
if(rear==front)
System.out.print("empty");
else
{
System.out.print("[ ");
for(int i=front;i<rear;i++)
System.out.print(queue1[i]+" ");
System.out.println("]");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 52

}
}
}

OUTPUT

Some thread is accessing the Queue...
1. Push element
2. Pop element
3. Display Stack
Enter choice...
1
1
Enter choice...
1
2
Enter choice...
1
3
Enter choice...
3
The queue is..
[ 1 2 3 ]
Enter choice...
2
The element dequeued is 1
Enter choice...
4
1. Push element
2. Pop element
3. Display Stack
Enter choice...
3
The queue is..
[ 2 3 ]
Enter choice...
1
4
Enter choice...
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 53

3
The queue is..
[ 2 3 4 ]
Enter choice...
5

RESULT Thus the code is written in Java and executed for accessing queue concurrently.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 <u, v>
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 <u, v>
Step 8.2.2.1: Assign distance as d(U) + c <u, v>
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
import java.util.*;
public class DynamicPrgm
{
static ArrayList<String> start = new ArrayList<String>();
static ArrayList<String> end = new ArrayList<String>();
static ArrayList<Integer> cost = new ArrayList<Integer>();
static ArrayList<String> nodes = new ArrayList<String>();
static int[] length = new int[20];
static ArrayList<String> handled = new ArrayList<String>();
static ArrayList<String> notHandled = new ArrayList<String>();
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 55

static int edgeCount;

public static void main(String[] arg)
{
int node;
int i;
Scanner s= new Scanner(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<edgeCount;i++)
{
System.out.println( "Enter the FROM node:");
System.out.println( "Enter the TO node:");
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:");
}
System.out.println( "Enter the cost of edge:");
if(!(nodes.contains(start.get(i))))
if(!(nodes.contains(end.get(i))))
}
//Initialising table values
for(i=0;i<nodes.size();i++)
if(i==0)
{ // for ROOT/SOURCE node
length[i]=0;
}
else // other nodes in the graph
length[i]=1000;
for(i=0;i<nodes.size();i++)
{
node=findMinimum();
findChild(node);
notHandled.clear();
}
System.out.println("NODE LENGTH");
print();
System.out.println("");
}

static int findMinimum()
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 56

3
5
7
2
6
4
p
q r
t s
{
int cost=1000;
int node=0;
for(int k=0; k<nodes.size(); k++)
if(cost>length[k] && !(handled.contains(nodes.get(k))))
{
cost=length[k];
node=k;
}
return node;
}

static void findChild(int node)
{
for(int j=0;j<edgeCount;j++)
if(nodes.get(node).equalsIgnoreCase(start.get(j)))
if(!(handled.contains(end.get(j))))
{
for(int k=0; k<nodes.size();k++)
{
if(end.get(j).equalsIgnoreCase(nodes.get(k)))
{
if(length[k]>(cost.get(j)+length[node]))
{
length[k]=cost.get(j)+length[node];
}
}
}

}
}

static void print()
{
for(int k=0;k<nodes.size();k++)
System.out.println(nodes.get(k)+" - "+length[k]);
}
}

GRAPH

U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package DynamicProgram;
import java.util.*;

public class RandomizedAlgm
{
static boolean flag=true;
static String choice;
static int n;
static int r;
public static void main(String[] arg)
{
Scanner s=new Scanner(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( " ");
choice=s.next();
if(choice.equals("p"))
{
System.out.println( "TO CALCULATE nPr :");
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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");
}
else if(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;
}
}
}

public static void findP(int n, int r)
{
// npr = n!/(n-r)!
int result=fact(n)/fact(n-r);
System.out.println( n+" P "+r+" = "+result);
}

public static void findC(int n, int r)
{
// ncr = n!/(n-r)!r!
int result=fact(n)/(fact(n-r)*fact(r));
System.out.println( n+" C "+r+" = "+result);
}

public static int fact(int n)
{
int fact=1;
for(int i=1;i<=n;i++)
{
fact=fact*i;
}
return fact;
}
}
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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

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

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

h
Wrong Choice...

RESULT Thus the code is written in Java and executed for a randomized algorithm to calculate
combination and permutation.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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
package DynamicProgram;
import java.util.*;

public class PhilosopherPrblm
{
static Scanner s=new Scanner(System.in);
static int[] chopStick={0,0,0,0,0};
static int philo;
static String choice;
static boolean flag=true;

public static void main(String[] arg)
{
System.out.println( "NOTE: Initally all philosophers are reading...");
while(flag)
{
System.out.println( "Select a philosopher..");
philo=s.nextInt();
if(philo<6 && philo>0)
{
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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)
{
case 1:
lock(0,1);
break;
case 2:
lock(1,2);
break;
case 3:
lock(2,3);
break;
case 4:
lock(3,4);
break;
case 5:
lock(4,0);
break;
}
}
else if(choice.equalsIgnoreCase("r"))
{
switch(philo)
{
case 1:
unlock(0,1);
break;
case 2:
unlock(1,2);
break;
case 3:
unlock(2,3);
break;
case 4:
unlock(3,4);
break;
case 5:
unlock(4,0);
break;
}
}
else
{
System.out.println( "Wrong choice.. The philosopher can either
flag=false;
}
job();
System.out.println( ".........................................");
}
else
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

Antonita Shilpa . J UIT ( FMCS02 ) 63

{
System.out.println( "There are only 5 philosophers on the table..");
flag=false;
}
}
}

static void lock(int m, int n)
{
if(chopStick[m]==0 && chopStick[n]==0)
{
chopStick[m]=1;
chopStick[n]=1;
}
else
{
System.out.println( "Wait till philosopher's neighbour finishes eating..");
System.out.println( " ");
}
}

static void unlock(int m,int n)
{
chopStick[m]=0;
chopStick[n]=0;
}

static void job()
{
for(int i=0;i<5;i++)
{
System.out.println( "Philosopher "+(i+1)+" is eating");
else
}
}
}

U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 2 is eating
.........................................
Select a philosopher..
4
Does he want to eat(e/E) / read(r/R)?
e
Philosopher 2 is eating
Philosopher 4 is eating
.........................................
Select a philosopher..

U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y

UNITED INSTITUTE OF TECHNOLOGY
Periyanaickenpalayam, Coimbatore- 641 020.
Department of Computer Science and Engineering

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 2 is eating
Philosopher 4 is eating
.........................................
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 dining
philosophers problem.
U
N
I
T
E
D

I
N
S
T
I
T
U
T
E

O
F

T
E
C
H
N
O
L
O
G
Y