public void Caculate(WeightedVertex origin) { float INFINITY = float.MaxValue; for (int i = 0; i < m_vertices.Count; ++i) { m_vertices[i].m_dist = INFINITY; m_vertices[i].m_known = false; } origin.m_dist = 0; WeightedVertex vert = null; for (; ;) { vert = GetMinDistVert(); if (vert == null) { break; } vert.m_known = true; foreach (var pair in vert.m_neighbours) { if (!pair.Key.m_known && pair.Value + vert.m_dist < pair.Key.m_dist) { pair.Key.m_dist = pair.Value + vert.m_dist; pair.Key.m_path = vert; } } } }
private WeightedVertex GetMinDistVert() { WeightedVertex vert = null; float min_dist = float.MaxValue; for (int i = 0; i < m_vertices.Count; ++i) { if (!m_vertices[i].m_known && min_dist > m_vertices[i].m_dist) { vert = m_vertices[i]; min_dist = vert.m_dist; } } return(vert); }
public void test() { WeightedVertex v1 = new WeightedVertex(1); WeightedVertex v2 = new WeightedVertex(2); WeightedVertex v3 = new WeightedVertex(3); WeightedVertex v4 = new WeightedVertex(4); WeightedVertex v5 = new WeightedVertex(5); WeightedVertex v6 = new WeightedVertex(6); WeightedVertex v7 = new WeightedVertex(7); v1.m_neighbours.Add(v4, 1); v1.m_neighbours.Add(v2, 2); v2.m_neighbours.Add(v4, 3); v2.m_neighbours.Add(v5, 10); v3.m_neighbours.Add(v1, 4); v3.m_neighbours.Add(v6, 5); v4.m_neighbours.Add(v5, 2); v4.m_neighbours.Add(v6, 8); v4.m_neighbours.Add(v7, 4); v4.m_neighbours.Add(v3, 2); v5.m_neighbours.Add(v7, 6); v7.m_neighbours.Add(v6, 1); m_vertices.Add(v1); m_vertices.Add(v2); m_vertices.Add(v3); m_vertices.Add(v4); m_vertices.Add(v5); m_vertices.Add(v6); m_vertices.Add(v7); m_vertices.Add(v1); Caculate(v1); }