Implementation (Part 1)
Lesson 5 of 9 in Coddy's Prim's Algorithm - Graph Algorithms course.
We start with the cheapest edge leaving a vertex, Prim's first move.
Challenge
EasyPrim's very first move is to look at the edges leaving the start vertex. Let's build that lookup.
Write a function named minEdgeFrom that takes the flat edges array (triples [u, v, w, ...], undirected) and a vertex node, and returns the smallest weight among all edges touching node. If node has no edges, return -1.
For example, minEdgeFrom([0,1,5, 0,2,3, 1,2,1], 0) returns 3.
Try it yourself
#include <stdlib.h>
int minEdgeFrom(int* edges, int edges_size, int node) {
// Write code here
return -1;
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Prim's Algorithm - Graph Algorithms
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)