Skip to content

Commit 5ea6a44

Browse files
committed
Merged Blog/Graph and DataStructure/Graph
Signed-off-by: Mayur Kulkarni <mayurkulkarni012@gmail.com>
1 parent 1662f81 commit 5ea6a44

File tree

1 file changed

+74
-11
lines changed

1 file changed

+74
-11
lines changed

DataStructures/Graph.java

Lines changed: 74 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,36 @@
66
* Created by Mayur Kulkarni on 2/5/2017.
77
*/
88
public class Graph<T> {
9+
/**
10+
* Creates a generic Graph
11+
*
12+
* Attributes:
13+
* size: represents size of the graph
14+
* graph: HashMap with key as node and value as their adjacency list
15+
* isUnidirectional: true if graph is unidirectional
16+
* time: used for articulation point, starts from 0, increments after
17+
* passing every vertex.
18+
*/
919
private int size;
1020
private Map<T, List<T>> graph;
1121
private boolean isUniDirectional;
1222
private int time = 0;
1323

24+
/**
25+
* Constructor of graph
26+
* @param size size of graph
27+
* @param isUniDirectional whether or not it is bidirectional.
28+
*/
1429
public Graph(int size, boolean isUniDirectional) {
1530
this.size = size;
1631
this.isUniDirectional = isUniDirectional;
1732
graph = new HashMap<>();
1833
}
1934

35+
/**
36+
* Run the program using custom graph!
37+
* @param args
38+
*/
2039
public static void main(String[] args) {
2140
Graph<Character> graph = new Graph<>(5, false);
2241
graph.addEdge('A', 'B');
@@ -25,6 +44,10 @@ public static void main(String[] args) {
2544
graph.printArticulationPoints();
2645
}
2746

47+
/**
48+
* Pretty printing
49+
* @return nice representation of graph
50+
*/
2851
@Override
2952
public String toString() {
3053
StringBuilder sb = new StringBuilder();
@@ -35,26 +58,44 @@ public String toString() {
3558
return sb.toString();
3659
}
3760

38-
private void insToMapGraph(T from, T to) {
39-
graph.compute(from, (key, value) -> {
61+
/**
62+
* Inserts toNode to the adjacenct list of fromNode
63+
*
64+
* @param fromNode from node
65+
* @param toNode to node
66+
*/
67+
private void insToMapGraph(T fromNode, T toNode) {
68+
graph.compute(fromNode, (key, value) -> {
4069
if (value == null) {
4170
List<T> adjList = new ArrayList<>();
42-
adjList.add(to);
71+
adjList.add(toNode);
4372
return adjList;
4473
} else {
45-
value.add(to);
74+
value.add(toNode);
4675
return value;
4776
}
4877
});
4978
}
5079

51-
public void addEdge(T from, T to) {
52-
insToMapGraph(from, to);
80+
/**
81+
* Adds an edge in graph. If current graph is bidirectional,
82+
* also adds edge from toNode to fromNode
83+
*
84+
* @param fromNode from node
85+
* @param toNode to node
86+
*/
87+
public void addEdge(T fromNode, T toNode) {
88+
insToMapGraph(fromNode, toNode);
5389
if (!isUniDirectional) {
54-
insToMapGraph(to, from);
90+
insToMapGraph(toNode, fromNode);
5591
}
5692
}
5793

94+
/**
95+
* Returns the adjacency list of the graph
96+
* @param key the index of node you're interested in
97+
* @return adjacency list of node specifief in key
98+
*/
5899
public List<T> adjacencyList(T key) {
59100
if (graph.containsKey(key)) {
60101
return graph.get(key);
@@ -63,6 +104,9 @@ public List<T> adjacencyList(T key) {
63104
}
64105
}
65106

107+
/**
108+
* Prints the articulation points of current graph
109+
*/
66110
public void printArticulationPoints() {
67111
Set<T> visited = new HashSet<T>();
68112
Map<T, Integer> visitedTime = new HashMap<T, Integer>();
@@ -75,6 +119,15 @@ public void printArticulationPoints() {
75119
System.out.println(articulationPoints);
76120
}
77121

122+
/**
123+
* DFS the graph represented by graph HashMap and find out articulation points
124+
* @param visited denotes the nodes which are already visited
125+
* @param visitedTime denotes the order in which nodes are visited
126+
* @param currNode the current node of this traversa;
127+
* @param lowTime denotes the minimum lowTime of adjacent vertices
128+
* @param parent parent of the current node
129+
* @param articulationPoints articulation points of the current graph
130+
*/
78131
public void DFS(Set<T> visited,
79132
Map<T, Integer> visitedTime,
80133
T currNode, Map<T, Integer> lowTime,
@@ -86,7 +139,7 @@ public void DFS(Set<T> visited,
86139
int childCount = 0;
87140
boolean isArticulationPoint = false;
88141
for (T adjNode : this.adjacencyList(currNode)) {
89-
142+
// remember, we ignore the parents of current node
90143
if (adjNode.equals(parent.get(currNode))) continue;
91144

92145
if (!visited.contains(adjNode)) {
@@ -102,13 +155,23 @@ public void DFS(Set<T> visited,
102155
lowTime.compute(currNode, (node, lowtime) -> Math.min(lowTime.get(currNode), lowTime.get(adjNode)));
103156
}
104157
}
105-
if ((isParent(currNode, parent) && childCount >= 2)
106-
|| (!isParent(currNode, parent) && isArticulationPoint)) {
158+
// first condition is satisfied only by root nodes of DFS, for rest of them there's
159+
// the second one
160+
if ((isRoot(currNode, parent) && childCount >= 2)
161+
|| (!isRoot(currNode, parent) && isArticulationPoint)) {
107162
articulationPoints.add(currNode);
108163
}
109164
}
110165

111-
private boolean isParent(T currNode, Map<T, T> parent) {
166+
/**
167+
* Tell whether the currNode is the root of the DFS or not.
168+
* The root of the DFS will have null as it's parent
169+
*
170+
* @param currNode The node you're interested in
171+
* @param parent parent HashMap who's key is node and value is it's parent
172+
* @return
173+
*/
174+
private boolean isRoot(T currNode, Map<T, T> parent) {
112175
return parent.get(currNode) == null;
113176
}
114177
}

0 commit comments

Comments
 (0)