Skip to content

Commit

Permalink
InOutTimeOfNodes
Browse files Browse the repository at this point in the history
  • Loading branch information
kalpak92 committed Feb 12, 2021
1 parent 08fd2f2 commit 809fd68
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ This repository contains the solutions to problems done from various resources f
- [x] [Is Graph Bipartite? - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/BipartiteGraph.java)
- [x] [Can Split everyone into two groups of any size - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/PossibleBipartition.java)
- [x] [Detect Cycle in an Undirected Graph - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Graph/CycleInUndirectedGraph.java)

- [x] [In out time of nodes - Leetcode]()

## Dynamic Programming
- [x] [Unique Paths : Basic - Leetcode](https://github.com/kalpak92/TechInterview2020/blob/master/src/Leetcode/UniquePaths.java)
Expand Down
71 changes: 71 additions & 0 deletions src/Graph/InOutTimeOfNodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package Graph;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author kalpak
*
* Given a graph, find the arrival and departure time of its vertices.
*/

public class InOutTimeOfNodes {
public static List<List<Integer>> createGraph(int numNodes) {
List<List<Integer>> graphAdjList = new ArrayList<>(numNodes);
for (int i = 0; i < numNodes; i++)
graphAdjList.add(new ArrayList<>());

return graphAdjList;
}

public static void addUndirectedEdge(List<List<Integer>> graph, int from, int to) {
graph.get(from).add(to);
graph.get(to).add(from);
}

public static int computeInOutTimeDFS(List<List<Integer>> graph, int startNode, boolean[] visited, int[] arrival, int[] departure, int time) {

// set the arrival time of vertex `v`
arrival[startNode] = ++time;

// mark vertex as discovered
visited[startNode] = true;

List<Integer> adjacencyList = graph.get(startNode);

for (int i: adjacencyList) {
if (!visited[i]) {
time = computeInOutTimeDFS(graph, i, visited, arrival, departure, time);
}
}

// set departure time of vertex `v`
departure[startNode] = ++time;
return time;
}

public static void main(String[] args) {
final int numNodes = 11;
List<List<Integer>> graph = createGraph(numNodes);

// Setup a graph with five connected components:
// {0,1,7}, {2,5}, {4,8}, {3,6,9}, {10}
addUndirectedEdge(graph, 0, 1);
addUndirectedEdge(graph, 1, 7);
addUndirectedEdge(graph, 7, 0);
addUndirectedEdge(graph, 2, 5);
addUndirectedEdge(graph, 4, 8);
addUndirectedEdge(graph, 3, 6);
addUndirectedEdge(graph, 6, 9);

int[] arrival = new int[numNodes];
int[] departure = new int[numNodes];

computeInOutTimeDFS(graph, 0, new boolean[numNodes], arrival, departure, 0);

System.out.println(Arrays.toString(arrival));
System.out.println(Arrays.toString(departure));
}
}

0 comments on commit 809fd68

Please sign in to comment.