Skip to content

Added Depth First Character Traversal for Graph as problem99 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/main/java/com/github/pedrovgs/problem99/Graph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import java.io.*;
import java.util.*;
class Graph
{
private int V;
private int R;
private LinkedList<Integer> adj[];
ArrayList<Integer> trav;
Graph(int v,int rel)
{
V = v;
R = 2*rel;
adj = new LinkedList[v];
trav = new ArrayList<>();
for (int i=0; i<v; ++i)
adj[i] = new LinkedList<Integer>();
}
void addEdge(int l, int m)
{
adj[l].add(new Integer(m));
adj[m].add(new Integer(l));
}
void DFSUtil(int v,boolean visited[])
{
visited[v] = true;
//System.out.print(v+" ");
trav.add(new Integer(v));
Iterator<Integer> i = adj[v].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
DFSUtil(n, visited);
}
}
ArrayList<Integer> DFS(int v)
{
boolean visited[] = new boolean[V];
DFSUtil(v, visited);

return trav;

}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of vertices and relations: ");
int n = sc.nextInt();
int rel = sc.nextInt();

sc.nextLine();

System.out.println("Enter all the vertices: ");
String family = sc.nextLine();
char fam[] = new char[n];
char sorted_fam[] = new char[n];
for(int j = 0 ; j < n ; j++) {
fam[j] = family.charAt(2*j);
sorted_fam[j] = fam[j];
}

Graph g = new Graph(n,rel);

Arrays.sort(sorted_fam);

System.out.println("Enter the two vertices in "+rel+" relations (space separated) : ");

for(int j = 0 ; j <rel ; j++) {
String relation = sc.nextLine();
char src = relation.charAt(0);
char dest = relation.charAt(2);
g.addEdge(Arrays.binarySearch(sorted_fam, src),Arrays.binarySearch(sorted_fam, dest));
}
System.out.println("Enter the source vertex: ");
int source = Arrays.binarySearch(sorted_fam, sc.nextLine().charAt(0));
ArrayList<Integer> ans = g.DFS(source);

for(int j = 0 ; j < ans.size() ; j++) {
System.out.print(sorted_fam[ans.get(j)] + " ");
}

}
}