Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41229ae

Browse files
Depth first search in (DFS) Java
1 parent e4dd9b7 commit 41229ae

File tree

22 files changed

+1432
-0
lines changed

22 files changed

+1432
-0
lines changed

‎Graph/depth-first-search/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Advanced Data Structures and Algorithms in Java
2+
3+
This ["Advanced Data Structures and Algorithms in Java"]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT) repository is for the students of my online course available on Udemy. It contains the source code of all the programs used in the course.
4+
5+
### About the Course
6+
* Thoroughly detailed course with complete working programs
7+
* Contains lots of animations to help you visualize the concepts
8+
* Includes AVL tree, BTree, Graph Algorithms
9+
* Builds a solid foundation in Data Structures and Algorithms
10+
* Prepares you for coding interviews
11+
* Lifetime Access
12+
13+
### Courses by [Deepali Srivastava](https://www.udemy.com/user/deepalisrivastava/)
14+
15+
[![data-structures- and-algorithms-in-java](https://user-images.githubusercontent.com/98641125/153196280-c2028f4b-d27b-432d-ad5a-9b04be2a3717.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java/?couponCode=GITHUBSTUDENT)
16+
[![data-structures- and-algorithms-in-java-2](https://user-images.githubusercontent.com/98641125/153196347-23003dc0-55b0-4315-8e52-425c51b2b5c4.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-java-2/?couponCode=GITHUBSTUDENT)
17+
[![data-structures- and-algorithms-in-c-plus-plus](https://user-images.githubusercontent.com/98641125/153196522-2412c993-1055-4322-8487-4133537566c9.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-c-plus-plus/?couponCode=GITHUBSTUDENT)
18+
[![data-structures- and-algorithms-in-python](https://user-images.githubusercontent.com/98641125/153196027-592d0307-5130-444f-8527-802634b5cc1e.png)]( https://www.udemy.com/course/data-structures-algorithms-in-python/?couponCode=GITHUBSTUDENT)
19+
[![python-programming-in-depth](https://user-images.githubusercontent.com/98641125/153196166-45ef8461-adb1-4f9f-b9ee-e482a5ad54a7.png)]( https://www.udemy.com/course/python-programming-in-depth/?couponCode=GITHUBSTUDENT)
20+
[![linux-commands](https://user-images.githubusercontent.com/98641125/153196567-96b3396c-8ee3-4233-b8fc-66c6b3bd830c.png)]( https://www.udemy.com/course/linux-commands/?couponCode=GITHUBSTUDENT)
21+
[![data-structures- and-algorithms-in-csharp](https://user-images.githubusercontent.com/98641125/153196407-99441e67-24a7-4fa0-aaea-78cb39743282.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp/?couponCode=GITHUBSTUDENT)
22+
[![data-structures- and-algorithms-in-csharp-2](https://user-images.githubusercontent.com/98641125/153196486-9ce09c97-8724-4492-b5d6-4bb4aeb1d8bd.png)]( https://www.udemy.com/course/data-structures-and-algorithms-in-csharp-2/?couponCode=GITHUBSTUDENT)
23+
24+
## Copyright
25+
© Copyright Deepali Srivastava : All rights reserved.
26+
Not to be used for commercial purposes.

‎Graph/depth-first-search/dfs/Demo.java

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package dfs;
7+
8+
public class Demo
9+
{
10+
public static void main(String [] args)
11+
{
12+
DirectedGraph g = new DirectedGraph();
13+
14+
g.insertVertex("Zero");
15+
g.insertVertex("One");
16+
g.insertVertex("Two");
17+
g.insertVertex("Three");
18+
g.insertVertex("Four");
19+
g.insertVertex("Five");
20+
g.insertVertex("Six");
21+
g.insertVertex("Seven");
22+
g.insertVertex("Eight");
23+
g.insertVertex("Nine");
24+
g.insertVertex("Ten");
25+
g.insertVertex("Eleven");
26+
27+
g.insertEdge("Zero","One");
28+
g.insertEdge("Zero","Three");
29+
g.insertEdge("One","Two");
30+
g.insertEdge("One","Four");
31+
g.insertEdge("One","Five");
32+
g.insertEdge("Two","Five");
33+
g.insertEdge("Two","Seven");
34+
g.insertEdge("Three","Six");
35+
g.insertEdge("Four","Three");
36+
g.insertEdge("Five","Three");
37+
g.insertEdge("Five","Six");
38+
g.insertEdge("Five","Eight");
39+
g.insertEdge("Seven","Eight");
40+
g.insertEdge("Seven","Ten");
41+
g.insertEdge("Eight","Eleven");
42+
g.insertEdge("Nine","Six");
43+
g.insertEdge("Eleven","Nine");
44+
45+
g.dfsTraversal();
46+
g.dfsTraversal_All();
47+
}
48+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package dfs;
7+
8+
import java.util.Scanner;
9+
import java.util.Stack;
10+
11+
public class DirectedGraph
12+
{
13+
public final int MAX_VERTICES = 30;
14+
15+
int n;
16+
int e;
17+
boolean [][] adj;
18+
Vertex [] vertexList;
19+
20+
private static final int INITIAL = 0;
21+
private static final int VISITED = 1;
22+
23+
public DirectedGraph()
24+
{
25+
adj = new boolean[MAX_VERTICES][MAX_VERTICES];
26+
vertexList = new Vertex[MAX_VERTICES];
27+
}
28+
29+
public void dfsTraversal()
30+
{
31+
for(int v=0; v<n; v++)
32+
vertexList[v].state = INITIAL;
33+
34+
Scanner scan = new Scanner(System.in);
35+
System.out.print("Enter starting vertex for Depth First Search : ");
36+
String s = scan.next();
37+
dfs(getIndex(s));
38+
}
39+
40+
private void dfs(int v)
41+
{
42+
Stack<Integer> st = new Stack<Integer>();
43+
st.push(v);
44+
while(!st.isEmpty())
45+
{
46+
v = st.pop();
47+
if(vertexList[v].state==INITIAL)
48+
{
49+
System.out.print(vertexList[v] + " ");
50+
vertexList[v].state=VISITED;
51+
}
52+
for(int i=n-1; i>=0; i--)
53+
{
54+
if(isAdjacent(v,i) && vertexList[i].state==INITIAL)
55+
st.push(i);
56+
}
57+
}
58+
System.out.println();
59+
}
60+
61+
public void dfsTraversal_All()
62+
{
63+
int v;
64+
for(v=0; v<n; v++)
65+
vertexList[v].state = INITIAL;
66+
67+
Scanner scan = new Scanner(System.in);
68+
System.out.print("Enter starting vertex for Depth First Search : ");
69+
String s = scan.next();
70+
dfs(getIndex(s));
71+
72+
for(v=0; v<n; v++)
73+
if(vertexList[v].state == INITIAL)
74+
dfs(v);
75+
}
76+
77+
private int getIndex(String s)
78+
{
79+
for(int i=0; i<n; i++)
80+
if(s.equals(vertexList[i].name))
81+
return i;
82+
throw new RuntimeException("Invalid Vertex");
83+
}
84+
85+
public void insertVertex(String name)
86+
{
87+
vertexList[n++] = new Vertex(name);
88+
}
89+
90+
91+
92+
private boolean isAdjacent(int u, int v)
93+
{
94+
return adj[u][v];
95+
}
96+
97+
/*Insert an edge (s1,s2) */
98+
public void insertEdge(String s1, String s2)
99+
{
100+
int u = getIndex(s1);
101+
int v = getIndex(s2);
102+
if(u==v)
103+
throw new IllegalArgumentException("Not a valid edge");
104+
if(adj[u][v] == true)
105+
System.out.print("Edge already present");
106+
else
107+
{
108+
adj[u][v]=true;
109+
e++;
110+
}
111+
}
112+
113+
114+
}
115+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package dfs;
7+
8+
public class Vertex
9+
{
10+
String name;
11+
int state;
12+
13+
Vertex(String name)
14+
{
15+
this.name = name;
16+
}
17+
public String toString()
18+
{
19+
return name;
20+
}
21+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package dfsClassifyEdges;
7+
8+
public class Demo
9+
{
10+
public static void main(String [] args)
11+
{
12+
DirectedGraph g = new DirectedGraph();
13+
14+
g.insertVertex("Zero");
15+
g.insertVertex("One");
16+
g.insertVertex("Two");
17+
g.insertVertex("Three");
18+
g.insertVertex("Four");
19+
g.insertVertex("Five");
20+
g.insertVertex("Six");
21+
g.insertVertex("Seven");
22+
g.insertVertex("Eight");
23+
g.insertVertex("Nine");
24+
g.insertVertex("Ten");
25+
g.insertVertex("Eleven");
26+
g.insertVertex("Twelve");
27+
g.insertVertex("Thirteen");
28+
g.insertVertex("Fourteen");
29+
g.insertVertex("Fifteen");
30+
g.insertVertex("Sixteen");
31+
32+
g.insertEdge("Zero","One");
33+
g.insertEdge("Zero","Two");
34+
g.insertEdge("Zero","Four");
35+
g.insertEdge("One","Three");
36+
g.insertEdge("Two","Three");
37+
g.insertEdge("Two","Four");
38+
g.insertEdge("Two","Five");
39+
//g.insertEdge("Three","Zero");
40+
g.insertEdge("Four","Five");
41+
g.insertEdge("Five","Three");
42+
g.insertEdge("Six","One");
43+
g.insertEdge("Six","Seven");
44+
g.insertEdge("Six","Eight");
45+
g.insertEdge("Six","Nine");
46+
g.insertEdge("Seven","Nine");
47+
g.insertEdge("Eight","Ten");
48+
g.insertEdge("Nine","Five");
49+
//g.insertEdge("Ten","Six");
50+
g.insertEdge("Ten","Nine");
51+
g.insertEdge("Eleven","Eight");
52+
g.insertEdge("Eleven","Thirteen");
53+
g.insertEdge("Eleven","Fifteen");
54+
//g.insertEdge("Twelve","Eleven");
55+
g.insertEdge("Thirteen","Eight");
56+
g.insertEdge("Thirteen","Fourteen");
57+
g.insertEdge("Thirteen","Fifteen");
58+
g.insertEdge("Thirteen","Sixteen");
59+
g.insertEdge("Fourteen","Sixteen");
60+
g.insertEdge("Fifteen","Twelve");
61+
g.insertEdge("Fifteen","Sixteen");
62+
63+
g.dfsTraversal_All();
64+
65+
if(g.isCyclic())
66+
System.out.println("Graph is Cyclic");
67+
else
68+
System.out.println("Graph is Acylic");
69+
}
70+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /