|
| 1 | +RSpec.describe DSA::Graph do |
| 2 | + describe "#add_vertex" do |
| 3 | + it "creates a new vertex" do |
| 4 | + graph = described_class.new |
| 5 | + |
| 6 | + vertex = graph.add_vertex(value: 42) |
| 7 | + |
| 8 | + expect(vertex).to be_a described_class::Vertex |
| 9 | + expect(vertex.value).to eq 42 |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + describe "#add_edge" do |
| 14 | + it "creates a new edge" do |
| 15 | + graph = described_class.new |
| 16 | + |
| 17 | + one = graph.add_vertex(value: 1) |
| 18 | + two = graph.add_vertex(value: 2) |
| 19 | + |
| 20 | + graph.add_edge(from: one, to: two) |
| 21 | + |
| 22 | + edges = graph.adjacency_list[one] |
| 23 | + expect(edges).to be_a DSA::SinglyLinkedList |
| 24 | + expect(edges.length).to eq 1 |
| 25 | + expect(edges[0].value).to be_a described_class::Vertex |
| 26 | + expect(edges[0].value.value).to eq 2 |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe "#topological_sort" do |
| 31 | + it "returns a sorted list of vertices" do |
| 32 | + graph = described_class.new |
| 33 | + |
| 34 | + cook_veggies = graph.add_vertex(value: :cook_veggies) |
| 35 | + cook_meat = graph.add_vertex(value: :cook_meat) |
| 36 | + prep_veggies = graph.add_vertex(value: :prep_veggies) |
| 37 | + prep_meat = graph.add_vertex(value: :prep_meat) |
| 38 | + cook_rice = graph.add_vertex(value: :cook_rice) |
| 39 | + serve_meal = graph.add_vertex(value: :serve_meal) |
| 40 | + buy_food = graph.add_vertex(value: :buy_food) |
| 41 | + |
| 42 | + graph.add_edge(from: buy_food, to: prep_veggies) |
| 43 | + graph.add_edge(from: buy_food, to: prep_meat) |
| 44 | + graph.add_edge(from: buy_food, to: cook_rice) |
| 45 | + |
| 46 | + graph.add_edge(from: prep_veggies, to: cook_veggies) |
| 47 | + graph.add_edge(from: prep_meat, to: cook_meat) |
| 48 | + |
| 49 | + graph.add_edge(from: cook_veggies, to: serve_meal) |
| 50 | + graph.add_edge(from: cook_meat, to: serve_meal) |
| 51 | + graph.add_edge(from: cook_rice, to: serve_meal) |
| 52 | + |
| 53 | + result = graph.topological_sort |
| 54 | + |
| 55 | + expect(result).to be_a Array |
| 56 | + expect(result[0]).to eq buy_food |
| 57 | + expect(result[1]).to eq cook_rice |
| 58 | + expect(result[2]).to eq prep_meat |
| 59 | + expect(result[3]).to eq prep_veggies |
| 60 | + expect(result[4]).to eq cook_meat |
| 61 | + expect(result[5]).to eq cook_veggies |
| 62 | + expect(result[6]).to eq serve_meal |
| 63 | + end |
| 64 | + |
| 65 | + it "errors if graph contains a cycle" do |
| 66 | + graph = described_class.new |
| 67 | + |
| 68 | + one = graph.add_vertex(value: 1) |
| 69 | + two = graph.add_vertex(value: 2) |
| 70 | + three = graph.add_vertex(value: 3) |
| 71 | + |
| 72 | + graph.add_edge(from: one, to: two) |
| 73 | + graph.add_edge(from: two, to: three) |
| 74 | + graph.add_edge(from: three, to: one) |
| 75 | + |
| 76 | + expect { graph.topological_sort } |
| 77 | + .to raise_error(ArgumentError, "graph cannot contain cycles") |
| 78 | + end |
| 79 | + end |
| 80 | +end |
0 commit comments