In: Computer Science
Language: Java or C (NO OTHER LANGUAGE)
Do NOT use Java library implementations of the data structures (queues, lists, STs, hashtables etc.)
Have a unit test implemented in main(). And comment every code.
Show examples from the executions.
Assume that the edges defined by the vertex pairs in the data base are one-way.
Question: Write a program that can answer if there is a path
between any to vertices.
For the vertex pairs use this as your input example:
AL FL
AL GA
AL MS
AL TN
AR LA
AR MO
AR MS
AR OK
AR TN
AR TX
AZ CA
To the comment: what do you mean with year? There is no year
The following code assumes there is a class called graph with parametes vertex and adjacency list (array not any other data structure).
int isthereEdge(String
src,
String
dst)
{
String[]
temp = new String[somenum];
boolean
visit[] =
new
boolean
[v];
String[]
queue = new String[somenum];
visit[s]=
true
;
queue.add(src);
while
(queue.size()!=
0
)
{
String
n;
i
= adj[src].listIterator();
while
(i.hasNext())
{
n
= i.next();
if
(n==dst)
return
1
;
if
(!visit[n])
{
visit[n]
=
true
;
queue.add(n);
}
}
}
return
0
;
}
Public static void main(String args[])
{
int ans = isthereEdge(source, destination);
if (ans)
{
System.out.println("exists");
}
else
{
System.out.println("Not exists");
}
}