Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 116303d

Browse files
committed
summary
1 parent db00a50 commit 116303d

File tree

4 files changed

+107
-7
lines changed

4 files changed

+107
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package DataAndAlgoL.Chp3LinkedLists;
2+
3+
//PROBLEM 40 EXCHANGE THE ADJACEN ELEMENTS
4+
public class ExchangeAdjacenNodes {
5+
public ListNode exchangeAdjNode(ListNode head){
6+
ListNode temp = new ListNode(0); //temporary node to point to head
7+
temp.next = head; //temp points its next node as head
8+
ListNode prev = temp, curr = head; // prev is temp node and current is the head
9+
while(curr != null && curr.next != null){ //while current has a head and current.next has a pointer to head's next node
10+
ListNode tmp = curr.next.next; //tmp becomes the 2 next nodes to curr
11+
curr.next.next = prev.next; //
12+
prev.next = curr.next;
13+
curr.next = tmp;
14+
prev = curr;
15+
curr = prev.next;
16+
}
17+
return temp.next;
18+
}
19+
}

DataAndAlgoL/Chpt9GraphAlgorithms/DFSGraph.java renamed to DataAndAlgoL/Chpt9GraphAlgorithms/DFSandBFSGraph.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package DataAndAlgoL.Chpt9GraphAlgorithms;
22

3+
import java.util.Queue;
34
import java.util.Stack;
45

56
class Vertex{
@@ -10,15 +11,16 @@ public Vertex(char lab){
1011
visited=false;
1112
}
1213
}
13-
public class DFSGraph {
14+
public class DFSandBFSGraph {
1415
final int maxVertices=20; //max number of vertices in the graph
1516
Vertex[] vertexList; //array of vertices
1617
int[][] adjMatrix; //matrix that will hold the vertices
17-
int vertexCount;
18-
Stack theStack;
18+
static int vertexCount;
19+
Stack theStack; //for dfs
20+
Queue theQueue;
1921

2022

21-
public DFSGraph(){
23+
public DFSandBFSGraph(){
2224
vertexList= new Vertex[maxVertices]; //new array of vertices of size maxVertices (20)
2325
adjMatrix= new int[maxVertices][maxVertices];// matrix that will contain the vertices with matrix size 20x20
2426
vertexCount=0;
@@ -67,6 +69,26 @@ public void dfs(){
6769
}
6870
}
6971

72+
public void bfs(){
73+
vertexList[0].visited=true; //first vertex assign visited in vertexList
74+
displayVertex(0); //displays the first vertex
75+
theQueue.add(0); //inserts 1st vertex in queue so we can go to next vertex and explore
76+
int v2;
77+
while(!theQueue.isEmpty()){
78+
int v1= (int) theQueue.remove();
79+
while((v2=getAdjUnvisitedVertex(v1)) !=1){
80+
vertexList[v2].visited=true;
81+
displayVertex(v2);
82+
theQueue.add(v2);
83+
}
84+
}
85+
86+
for(int j=0; j< vertexCount; j++){
87+
vertexList[j].visited=false;
88+
}
89+
90+
}
91+
7092
public int getAdjUnvisitedVertex(int v){
7193
for(int i=0; i< vertexCount; i++){
7294
if(adjMatrix[v][i]==1 && vertexList[i].visited==false){

DataAndAlgoL/Chpt9GraphAlgorithms/Graph.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void clearVisited(){
127127
visited[i] = false;
128128
}
129129

130-
//ALGORITHM IMPORTANT PART
130+
//ALGORITHM IMPORTANT PART DFS ALGORITHM
131131
public void dfs(){
132132
//visit nodes using a stack to store "to visit" nodes
133133
Stack<Integer> s= new Stack<>();
@@ -151,11 +151,36 @@ public void dfs(){
151151
}
152152
}
153153

154+
//BFS ALGORITHM IMPORTANT
155+
public void bfs(){
156+
Queue<Integer> q = new LinkedList<>(); //BFS USES QUEUE DATA SRUCTURE
157+
clearVisited();
158+
q.add(0);
159+
160+
//LOOP AS LONG AS THERE IS AN ACTIVE NODE
161+
while(!q.isEmpty()){
162+
int nextNode =q.remove(); // NEXT NODE TO VISIT
163+
if(!visited[nextNode]){
164+
visited[nextNode]=true; //MARK NODE/VERTEX AS VISITED
165+
System.out.println("nextNode= "+nextNode); //PRINT CURRENT NODE STARTING FROM FIRST NODE/VERTEX
166+
for(int i=0; i< V; i++){
167+
if(adjMatrix[nextNode][i] ==true && !visited[i]){ //IF NEXTNODE ON THE SAME ROW BUT NEXT COLUMN IS TRUE AND NOT VISITED ADD THAT NODE TO THE QUEUE TO EXPLORE THE NEXT VERTICES
168+
q.add(i);
169+
}
170+
}
171+
}
172+
}
173+
}
174+
154175
public static void main(String[] args) {
155-
int V =10;
156-
int E =13;
176+
int V =5;
177+
int E =7;
157178
Graph G= new Graph(V,E);
158179
System.out.println(G.toString());
180+
System.out.println("DFS ALGORITHM");
159181
G.dfs();
182+
System.out.println();
183+
System.out.println("BFS ALGORITHM");
184+
G.bfs();
160185
}
161186
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package DataAndAlgoL.Chpt9GraphAlgorithms;
2+
import java.util.*;
3+
import DataAndAlgoL.Chpt9GraphAlgorithms.DFSandBFSGraph;
4+
//sole purpose of avoiding the error, still needs implementation
5+
class LLQueue{
6+
7+
}
8+
public class topologicalSort {
9+
// public void TopologicalSort(DFSandBFSGraph G){
10+
// LLQueue Q= new LLQueue(); //linked list queue
11+
// int counter=0;
12+
// int v, w;
13+
14+
// for(int i =0; i< DFSandBFSGraph.vertexCount; i++){
15+
// if(indegree[v]==0){
16+
// Q.deQueue(v);
17+
// }
18+
// }
19+
20+
// while(!Q.isEmpty()){
21+
// v=Q.deQueue();
22+
// topologicalOrder[v]=counter++;
23+
// for each w adjacent to v
24+
// if(--indegree[w]==0){
25+
// Q.enQueue(w);
26+
// }
27+
// }
28+
// if(counter != G.vertexCount){
29+
// System.out.println("Graph has a cycle");
30+
// }
31+
32+
// Q.deleteQueue();
33+
// }
34+
}

0 commit comments

Comments
 (0)