|
| 1 | +package graph |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestPrimMST(t *testing.T) { |
| 10 | + |
| 11 | + var testCases = []struct { |
| 12 | + edges []Edge |
| 13 | + vertices int |
| 14 | + start int |
| 15 | + cost int |
| 16 | + mst []Edge |
| 17 | + }{ |
| 18 | + { |
| 19 | + edges: []Edge{ |
| 20 | + {Start: 0, End: 1, Weight: 4}, |
| 21 | + {Start: 0, End: 2, Weight: 13}, |
| 22 | + {Start: 0, End: 3, Weight: 7}, |
| 23 | + {Start: 0, End: 4, Weight: 7}, |
| 24 | + {Start: 1, End: 2, Weight: 9}, |
| 25 | + {Start: 1, End: 3, Weight: 3}, |
| 26 | + {Start: 1, End: 4, Weight: 7}, |
| 27 | + {Start: 2, End: 3, Weight: 10}, |
| 28 | + {Start: 2, End: 4, Weight: 14}, |
| 29 | + {Start: 3, End: 4, Weight: 4}, |
| 30 | + }, |
| 31 | + vertices: 5, |
| 32 | + start: 0, |
| 33 | + cost: 20, |
| 34 | + mst: []Edge{ |
| 35 | + {Start: 0, End: 1, Weight: 4}, |
| 36 | + {Start: 1, End: 3, Weight: 3}, |
| 37 | + {Start: 3, End: 4, Weight: 4}, |
| 38 | + {Start: 1, End: 2, Weight: 9}, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + edges: []Edge{ |
| 43 | + {Start: 0, End: 1, Weight: 4}, |
| 44 | + {Start: 0, End: 7, Weight: 8}, |
| 45 | + {Start: 1, End: 2, Weight: 8}, |
| 46 | + {Start: 1, End: 7, Weight: 11}, |
| 47 | + {Start: 2, End: 3, Weight: 7}, |
| 48 | + {Start: 2, End: 5, Weight: 4}, |
| 49 | + {Start: 2, End: 8, Weight: 2}, |
| 50 | + {Start: 3, End: 4, Weight: 9}, |
| 51 | + {Start: 3, End: 5, Weight: 14}, |
| 52 | + {Start: 4, End: 5, Weight: 10}, |
| 53 | + {Start: 5, End: 6, Weight: 2}, |
| 54 | + {Start: 6, End: 7, Weight: 1}, |
| 55 | + {Start: 6, End: 8, Weight: 6}, |
| 56 | + {Start: 7, End: 8, Weight: 7}, |
| 57 | + }, |
| 58 | + vertices: 9, |
| 59 | + start: 3, |
| 60 | + cost: 37, |
| 61 | + mst: []Edge{ |
| 62 | + {Start: 3, End: 2, Weight: 7}, |
| 63 | + {Start: 2, End: 8, Weight: 2}, |
| 64 | + {Start: 2, End: 5, Weight: 4}, |
| 65 | + {Start: 5, End: 6, Weight: 2}, |
| 66 | + {Start: 6, End: 7, Weight: 1}, |
| 67 | + {Start: 2, End: 1, Weight: 8}, |
| 68 | + {Start: 1, End: 0, Weight: 4}, |
| 69 | + {Start: 3, End: 4, Weight: 9}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + edges: []Edge{ |
| 74 | + {Start: 0, End: 1, Weight: 2}, |
| 75 | + {Start: 0, End: 3, Weight: 6}, |
| 76 | + {Start: 1, End: 2, Weight: 3}, |
| 77 | + {Start: 1, End: 3, Weight: 8}, |
| 78 | + {Start: 1, End: 4, Weight: 5}, |
| 79 | + {Start: 2, End: 4, Weight: 7}, |
| 80 | + {Start: 3, End: 4, Weight: 9}, |
| 81 | + }, |
| 82 | + vertices: 5, |
| 83 | + start: 2, |
| 84 | + cost: 16, |
| 85 | + mst: []Edge{ |
| 86 | + {Start: 2, End: 1, Weight: 3}, |
| 87 | + {Start: 1, End: 0, Weight: 2}, |
| 88 | + {Start: 1, End: 4, Weight: 5}, |
| 89 | + {Start: 0, End: 3, Weight: 6}, |
| 90 | + }, |
| 91 | + }, |
| 92 | + { |
| 93 | + edges: []Edge{ |
| 94 | + {Start: 0, End: 0, Weight: 0}, |
| 95 | + }, |
| 96 | + vertices: 1, |
| 97 | + start: 0, |
| 98 | + cost: 0, |
| 99 | + mst: nil, |
| 100 | + }, |
| 101 | + { |
| 102 | + edges: []Edge{ |
| 103 | + {Start: 0, End: 1, Weight: 1}, |
| 104 | + {Start: 0, End: 2, Weight: 6}, |
| 105 | + {Start: 0, End: 3, Weight: 5}, |
| 106 | + {Start: 1, End: 2, Weight: 2}, |
| 107 | + {Start: 1, End: 4, Weight: 4}, |
| 108 | + {Start: 2, End: 4, Weight: 9}, |
| 109 | + }, |
| 110 | + vertices: 5, |
| 111 | + start: 4, |
| 112 | + cost: 12, |
| 113 | + mst: []Edge{ |
| 114 | + {Start: 4, End: 1, Weight: 4}, |
| 115 | + {Start: 1, End: 0, Weight: 1}, |
| 116 | + {Start: 1, End: 2, Weight: 2}, |
| 117 | + {Start: 0, End: 3, Weight: 5}, |
| 118 | + }, |
| 119 | + }, |
| 120 | + } |
| 121 | + |
| 122 | + for i, testCase := range testCases { |
| 123 | + t.Run(fmt.Sprintf("Test Case %d", i), func(t *testing.T) { |
| 124 | + // Initializing graph, adding edges |
| 125 | + graph := New(testCase.vertices) |
| 126 | + graph.Directed = false |
| 127 | + for _, edge := range testCase.edges { |
| 128 | + graph.AddWeightedEdge(int(edge.Start), int(edge.End), edge.Weight) |
| 129 | + } |
| 130 | + |
| 131 | + computedMST, computedCost := graph.PrimMST(Vertex(testCase.start)) |
| 132 | + |
| 133 | + // Compare the computed result with the expected result |
| 134 | + if computedCost != testCase.cost { |
| 135 | + t.Errorf("Test Case %d, Expected Cost: %d, Computed: %d", i, testCase.cost, computedCost) |
| 136 | + } |
| 137 | + |
| 138 | + if !reflect.DeepEqual(testCase.mst, computedMST) { |
| 139 | + t.Errorf("Test Case %d, Expected MST: %v, Computed: %v", i, testCase.mst, computedMST) |
| 140 | + } |
| 141 | + }) |
| 142 | + } |
| 143 | +} |
0 commit comments