|
| 1 | +package graph |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +type dijkstraTestCase struct { |
| 8 | + graph []*dijkstraVertex |
| 9 | + distances []int |
| 10 | +} |
| 11 | + |
| 12 | +/* |
| 13 | +TestDijkstra tests solution(s) with the following signature and problem description: |
| 14 | +
|
| 15 | + dijkstraVertex struct { |
| 16 | + val int |
| 17 | + distance int |
| 18 | + discovered bool |
| 19 | + edges []*weightedEdge |
| 20 | + } |
| 21 | + weightedEdge struct { |
| 22 | + edge *dijkstraVertex |
| 23 | + weight int |
| 24 | + } |
| 25 | + func Dijkstra(graph []*dijkstraVertex, source *dijkstraVertex) |
| 26 | +
|
| 27 | +Implement Dijkstra's algorithm. Given a single sourced edge weighted graph, find the |
| 28 | +shortest path from the source of the graph to every node and set it to the distance |
| 29 | +field of the node. |
| 30 | +
|
| 31 | + 5 |
| 32 | + (4) ↖ (1) |
| 33 | + 2 ─────→ 4 |
| 34 | + (1) ↑ (2) ↗ (1) |
| 35 | + 1 ──→ 3 |
| 36 | +
|
| 37 | +For the above graph, the shortest paths for each node are {0, 1, 3, 5, 8}. |
| 38 | +*/ |
| 39 | +func TestDijkstra(t *testing.T) { |
| 40 | + tests := []*dijkstraTestCase{ |
| 41 | + { |
| 42 | + graph: newGraph(2), |
| 43 | + distances: []int{0, 10}, |
| 44 | + }, |
| 45 | + { |
| 46 | + graph: newGraph(5), |
| 47 | + distances: []int{0, 1, 3, 5, 8}, |
| 48 | + }, |
| 49 | + { |
| 50 | + graph: newGraph(4), |
| 51 | + distances: []int{0, 5, 3, 10}, |
| 52 | + }, |
| 53 | + { |
| 54 | + graph: newGraph(3), |
| 55 | + distances: []int{0, 3, 5}, |
| 56 | + }, |
| 57 | + { |
| 58 | + graph: newGraph(6), |
| 59 | + distances: []int{0, 1, 3, 4, 6, 10}, |
| 60 | + }, |
| 61 | + } |
| 62 | + addEdges(tests) |
| 63 | + |
| 64 | + for i, test := range tests { |
| 65 | + Dijkstra(test.graph, test.graph[0]) |
| 66 | + for j, v := range test.graph { |
| 67 | + if v.distance != test.distances[j] { |
| 68 | + t.Errorf("Test Case %d, Vertex %d distance: got %d, expected %d", i, v.val, v.distance, test.distances[j]) |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func newGraph(edges int) []*dijkstraVertex { |
| 75 | + graph := make([]*dijkstraVertex, edges) |
| 76 | + for i := 0; i < edges; i++ { |
| 77 | + graph[i] = &dijkstraVertex{val: i} |
| 78 | + } |
| 79 | + return graph |
| 80 | +} |
| 81 | + |
| 82 | +func addEdges(tests []*dijkstraTestCase) { |
| 83 | + tests[0].graph[0].edges = []*weightedEdge{{edge: tests[0].graph[1], weight: 10}} |
| 84 | + |
| 85 | + tests[1].graph[0].edges = []*weightedEdge{{edge: tests[1].graph[1], weight: 1}, {edge: tests[1].graph[2], weight: 3}} |
| 86 | + tests[1].graph[1].edges = []*weightedEdge{{edge: tests[1].graph[2], weight: 2}, {edge: tests[1].graph[3], weight: 4}} |
| 87 | + tests[1].graph[2].edges = []*weightedEdge{{edge: tests[1].graph[3], weight: 3}, {edge: tests[1].graph[4], weight: 5}} |
| 88 | + tests[1].graph[3].edges = []*weightedEdge{{edge: tests[1].graph[4], weight: 5}} |
| 89 | + |
| 90 | + tests[2].graph[0].edges = []*weightedEdge{{edge: tests[2].graph[1], weight: 5}, {edge: tests[2].graph[2], weight: 3}} |
| 91 | + tests[2].graph[1].edges = []*weightedEdge{{edge: tests[2].graph[2], weight: 2}, {edge: tests[2].graph[3], weight: 6}} |
| 92 | + tests[2].graph[2].edges = []*weightedEdge{{edge: tests[2].graph[3], weight: 7}} |
| 93 | + |
| 94 | + tests[3].graph[0].edges = []*weightedEdge{{edge: tests[3].graph[1], weight: 3}, {edge: tests[3].graph[2], weight: 5}} |
| 95 | + tests[3].graph[1].edges = []*weightedEdge{{edge: tests[3].graph[2], weight: 2}} |
| 96 | + |
| 97 | + tests[4].graph[0].edges = []*weightedEdge{{edge: tests[4].graph[1], weight: 1}, {edge: tests[4].graph[2], weight: 4}} |
| 98 | + tests[4].graph[1].edges = []*weightedEdge{{edge: tests[4].graph[2], weight: 2}, {edge: tests[4].graph[3], weight: 5}} |
| 99 | + tests[4].graph[2].edges = []*weightedEdge{{edge: tests[4].graph[3], weight: 1}, {edge: tests[4].graph[4], weight: 3}} |
| 100 | + tests[4].graph[3].edges = []*weightedEdge{{edge: tests[4].graph[4], weight: 2}, {edge: tests[4].graph[1], weight: 1}} |
| 101 | + tests[4].graph[4].edges = []*weightedEdge{{edge: tests[4].graph[5], weight: 4}} |
| 102 | +} |
0 commit comments