Skip to content

Commit ddf149b

Browse files
committed
Update README.
1 parent dc2b7ce commit ddf149b

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
* [Dijkstra Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/dijkstra) - finding shortest path to all graph vertices
7070
* [Bellman-Ford Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices
7171
* [Detect Cycle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/detect-cycle) - for both directed and undirected graphs (DFS and Disjoint Set based versions)
72-
* Prim’s Algorithm - finding Minimum Spanning Tree (MST)
72+
* [Prim’s Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST)
7373
* Kruskal’s Algorithm - finding Minimum Spanning Tree (MST)
7474
* Topological Sorting
7575
* Eulerian path, Eulerian circuit
@@ -85,6 +85,7 @@
8585
* **Greedy**
8686
* [Unbound Knapsack Problem](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/knapsack-problem)
8787
* [Dijkstra Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/dijkstra) - finding shortest path to all graph vertices
88+
* [Prim’s Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST)
8889
* **Divide and Conquer**
8990
* [Euclidean Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
9091
* [Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/permutations) (with and without repetitions)

src/algorithms/graph/prim/README.md

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Prim's Algorithm
2+
3+
In computer science, **Prim's algorithm** is a greedy algorithm that
4+
finds a minimum spanning tree for a weighted undirected graph.
5+
6+
The algorithm operates by building this tree one vertex at a
7+
time, from an arbitrary starting vertex, at each step adding
8+
the cheapest possible connection from the tree to another vertex.
9+
10+
![Prim's Algorithm](https://upload.wikimedia.org/wikipedia/commons/f/f7/Prim%27s_algorithm.svg)
11+
12+
Prim's algorithm starting at vertex `A`. In the third step, edges
13+
`BD` and `AB` both have weight `2`, so `BD` is chosen arbitrarily.
14+
After that step, `AB` is no longer a candidate for addition
15+
to the tree because it links two nodes that are already
16+
in the tree.
17+
18+
## Minimum Spanning Tree
19+
20+
A **minimum spanning tree** (MST) or minimum weight spanning tree
21+
is a subset of the edges of a connected, edge-weighted
22+
(un)directed graph that connects all the vertices together,
23+
without any cycles and with the minimum possible total edge
24+
weight. That is, it is a spanning tree whose sum of edge weights
25+
is as small as possible. More generally, any edge-weighted
26+
undirected graph (not necessarily connected) has a minimum
27+
spanning forest, which is a union of the minimum spanning
28+
trees for its connected components.
29+
30+
![Minimum Spanning Tree](https://upload.wikimedia.org/wikipedia/commons/d/d2/Minimum_spanning_tree.svg)
31+
32+
A planar graph and its minimum spanning tree. Each edge is
33+
labeled with its weight, which here is roughly proportional
34+
to its length.
35+
36+
![Minimum Spanning Tree](https://upload.wikimedia.org/wikipedia/commons/c/c9/Multiple_minimum_spanning_trees.svg)
37+
38+
This figure shows there may be more than one minimum spanning
39+
tree in a graph. In the figure, the two trees below the graph
40+
are two possibilities of minimum spanning tree of the given graph.
41+
42+
## References
43+
44+
- [Minimum Spanning Tree on Wikipedia](https://en.wikipedia.org/wiki/Minimum_spanning_tree)
45+
- [Prim's Algorithm on Wikipedia](https://en.wikipedia.org/wiki/Prim%27s_algorithm)
46+
- [Prim's Algorithm on YouTube by Tushar Roy](https://www.youtube.com/watch?v=oP2-8ysT3QQ)

0 commit comments

Comments
 (0)