Skip to content

Commit 3ea1ce7

Browse files
committed
update: added method to test Graph is connected or not, Tree or not
1 parent aca1231 commit 3ea1ce7

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

β€ŽInterview Prep/section20_Graph/Graph.java

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,4 +442,156 @@ public boolean isCyclic() {
442442
return false;
443443
}
444444

445+
public boolean isConnected() {
446+
447+
int flag = 0; // to count no. of components in Graph
448+
449+
HashMap<String, Boolean> processed = new HashMap<>();
450+
451+
// use addLast() and removeFirst() of LinkedList - O(1) Time operations
452+
LinkedList<Pair> queue = new LinkedList<>(); // Queue using LinkedList
453+
454+
ArrayList<String> key = new ArrayList<>(this.vertices.keySet());
455+
456+
// looping all vertices/nodes/keys
457+
for (String node : key) {
458+
459+
// if graph is disconnected, handle that case
460+
// runs for all components of the Graph
461+
if (processed.containsKey(node)) {
462+
continue;
463+
}
464+
465+
flag++;
466+
467+
// create a new Pair
468+
Pair srcPair = new Pair();
469+
srcPair.vname = node;
470+
srcPair.pathSoFar = node;
471+
472+
// put the new pair in queue
473+
queue.addLast(srcPair);
474+
475+
// while queue is not empty keep on doing the work
476+
while (!queue.isEmpty()) {
477+
478+
// remove a pair from the queue
479+
Pair removePair = queue.removeFirst();
480+
481+
// check if that vertex is already processed
482+
if (processed.containsKey(removePair.vname)) {
483+
continue;
484+
}
485+
486+
processed.put(removePair.vname, true);
487+
488+
// devote work to neighbors
489+
490+
Vertex removePairVertex = this.vertices.get(removePair.vname); // address
491+
// all neighbors of removedPair vertex
492+
ArrayList<String> removePairNbrs = new ArrayList<>(removePairVertex.nbrs.keySet());
493+
494+
// loop on neighbors
495+
for (String nbr : removePairNbrs) {
496+
497+
// process only unprocessed neighbors
498+
if (!processed.containsKey(nbr)) {
499+
500+
// make a new pair of neighbor & put it in Queue
501+
Pair newPair = new Pair();
502+
newPair.vname = nbr;
503+
newPair.pathSoFar = removePair.pathSoFar + nbr;
504+
505+
queue.addLast(newPair);
506+
}
507+
}
508+
}
509+
}
510+
511+
// flag == 1 denotes, Graph has only one component
512+
if (flag >= 2) {
513+
return false;
514+
} else {
515+
return true;
516+
}
517+
}
518+
519+
// Tree is an acyclic connected graph
520+
521+
public boolean isTree2() {
522+
523+
int flag = 0; // to check if Graph is connected or not
524+
525+
HashMap<String, Boolean> processed = new HashMap<>();
526+
527+
// use addLast() and removeFirst() of LinkedList - O(1) Time operations
528+
LinkedList<Pair> queue = new LinkedList<>(); // Queue using LinkedList
529+
530+
ArrayList<String> key = new ArrayList<>(this.vertices.keySet());
531+
532+
// looping all vertices/nodes/keys
533+
for (String node : key) {
534+
535+
// if graph is disconnected, handle that case
536+
// runs for all components of the Graph
537+
if (processed.containsKey(node)) {
538+
continue;
539+
}
540+
541+
flag++;
542+
543+
// create a new Pair
544+
Pair srcPair = new Pair();
545+
srcPair.vname = node;
546+
srcPair.pathSoFar = node;
547+
548+
// put the new pair in queue
549+
queue.addLast(srcPair);
550+
551+
// while queue is not empty keep on doing the work
552+
while (!queue.isEmpty()) {
553+
554+
// remove a pair from the queue
555+
Pair removePair = queue.removeFirst();
556+
557+
// check if that vertex is already processed, if so then cycle
558+
// is present, for tree, it should be acyclic
559+
if (processed.containsKey(removePair.vname)) {
560+
return false;
561+
}
562+
563+
processed.put(removePair.vname, true);
564+
565+
// devote work to neighbors
566+
567+
Vertex removePairVertex = this.vertices.get(removePair.vname); // address
568+
// all neighbors of removedPair vertex
569+
ArrayList<String> removePairNbrs = new ArrayList<>(removePairVertex.nbrs.keySet());
570+
571+
// loop on neighbors
572+
for (String nbr : removePairNbrs) {
573+
574+
// process only unprocessed neighbors
575+
if (!processed.containsKey(nbr)) {
576+
577+
// make a new pair of neighbor & put it in Queue
578+
Pair newPair = new Pair();
579+
newPair.vname = nbr;
580+
newPair.pathSoFar = removePair.pathSoFar + nbr;
581+
582+
queue.addLast(newPair);
583+
}
584+
}
585+
}
586+
}
587+
if (flag >= 2) {
588+
return false;
589+
} else {
590+
return true;
591+
}
592+
}
593+
594+
public boolean isTree() {
595+
return !isCyclic() && isConnected();
596+
}
445597
}

0 commit comments

Comments
Β (0)