Graph Learning (17 Part Series)
1 Breadth First Search Traversal of Graph GeeksForGeeks
2 Topological Sorting in Graph Using DFS and BFS GeeksForGeeks
… 13 more parts…
3 Check if a graph is Bipartite or not Leetcode
4 Cycle detection in a graph using Breadh First Search and Depth First Search GeeksForGeeks
5 Depth first search GeeksForGeeks
6 Shortest Distance from source to all other nodes in the graph where the edge weight is 1 unit
7 Dijkstra’s single source shortest path algorithm
8 Minimum Spanning Tree (Prims Algorithm)
9 Minimum Spanning Tree (Kruskal’s algorithm) Using Disjoint set
10 Bridges in the graph
11 Strongly connected components in the graph
12 Bellman ford algorithm(Single Source Shorted Path in DAG)
13 Floyd Warshall Algorithm (Multi-source shorted path)
14 Disjoint Set Graph Learning
15 Disjoint set graph with union by rank and union by size
16 Number of islands
17 Find safest walk through the grid
Given a weighted, undirected and connected graph of V vertices and E edges. The task is to find the sum of weights of the edges of the Minimum Spanning Tree.
Example 1:
The Spanning Tree resulting in a weight
of 4 is shown above.
class Solution
{
//Function to find sum of weights of edges of the Minimum Spanning Tree.
// its similar to dijkstra's single source shortest path algorithm
static int spanningTree(int V, ArrayList<ArrayList<ArrayList<Integer>>> adj)
{
// Add your code here
boolean mstSet[] = new boolean[adj.size()];// this is nothing but visited nodes , that have become part of already chosen
int key[] = new int[adj.size()];
Arrays.fill(key,1000000000);// writing 1e9 means the nodes are yet to be discovered
key[0] =0; // node 0
PriorityQueue<Pair> q = new PriorityQueue<>((a,b)->a.getValue()-b.getValue());
//let the starting node be 0
q.add(new Pair(key[0],0)); //distance of 0 from 0 is 0
while(!q.isEmpty()){
Pair p = q.remove();
mstSet[p.getKey()] = true;
//System.out.println("node is "+p.getKey() + " d from node 0 is "+ p.getValue());
for(List<Integer> l : adj.get(p.getKey())){
// below if statement will mean that this adjacent node of node p.getKey() has not been taken
if(mstSet[l.get(0)]== false && key[l.get(0)] > l.get(1)){
key[l.get(0)] = l.get(1);
q.add(new Pair(l.get(0),l.get(1)));
}
}
}
//for(int i : mstSet) System.out.print(i+" ");
return Arrays.stream(key).reduce(0,(a,b)->a+b);
}
}
Enter fullscreen mode Exit fullscreen mode
Graph Learning (17 Part Series)
1 Breadth First Search Traversal of Graph GeeksForGeeks
2 Topological Sorting in Graph Using DFS and BFS GeeksForGeeks
… 13 more parts…
3 Check if a graph is Bipartite or not Leetcode
4 Cycle detection in a graph using Breadh First Search and Depth First Search GeeksForGeeks
5 Depth first search GeeksForGeeks
6 Shortest Distance from source to all other nodes in the graph where the edge weight is 1 unit
7 Dijkstra’s single source shortest path algorithm
8 Minimum Spanning Tree (Prims Algorithm)
9 Minimum Spanning Tree (Kruskal’s algorithm) Using Disjoint set
10 Bridges in the graph
11 Strongly connected components in the graph
12 Bellman ford algorithm(Single Source Shorted Path in DAG)
13 Floyd Warshall Algorithm (Multi-source shorted path)
14 Disjoint Set Graph Learning
15 Disjoint set graph with union by rank and union by size
16 Number of islands
17 Find safest walk through the grid
暂无评论内容