So I need to solve this graph, I have a general idea on how to solve it but if I am doing this wrong please correct me.
So to find the MST I need to perform Kruskals algorithm on the graph
here is my psuedocode for this Kruskals algorithm
Kruskal(V,E) A = null; for each v contains in V make disjoint set(v) Sort E by weight increasingly for each(v1, v2) contains in E if
Kruskal(V,E)A = null; for each v contains in V make disjoint set(v) Sort E by weight increasingly for each(v1, v2) contains in E if Find(v1) is not equal to Find(v2) A = A Union {(v1,v2)} Union(v1,v2) Return A
So first thing I do is find the nodes with the shortest distances right?
1)
I assume S to H has the shortest distance since the d(h,s) = -3
so A = {(h,s)}
so now we follow this pattern
2) A = {(h,s),(s,f)}
3) A = {(h,s),(s,f)(s,n)}
4) A = {(h,s)(s,f)(s,n)(f,k)}
5) A = {(h,s)(s,f)(s,n)(f,k)(s,m)} (we skip H to N because a path is already made from h to n which is through s )
6) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)}
7) A = {(h,s)(s,f)(s,n)(f,k)(s,m)(d,b)(b,m)}
so now since there is a path that connects to all edges we are good right?
but the thing i dont understand is there there are distance[u,v] which is shorter than the path[u,v] through multiple vertices. For example d[d,m] is shorter then p[d,m] which goes through B first. Am I doing something wrong?
You're not doing anything wrong. There's no guarantee that a MST preserves shortest distances between nodes. Eg: the three node complete graph ABC with edge weights 3, 2, 2 (with apologies for my ascii art):
A --- 2 --- B
| |
2 /
| /
C----3---/
The minimal spanning tree is C-A-B but the distance between B and C in the original graph is 3, and in the MST 4.