Class: Graphunk::WeightedDirectedGraph
Instance Attribute Summary
#weights
Instance Method Summary
collapse
#initialize
Methods inherited from Graph
#add_vertex, #add_vertices, #edges, #edges_on_vertex, #initialize, #remove_vertex, #vertex_exists?, #vertices
Instance Method Details
#add_edge(v, u, w) ⇒ Object
4
5
6
7
8
9
10
11
12
13
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 4
def add_edge(v, u, w)
if edge_exists?(v, u)
raise ArgumentError, "This edge already exists"
elsif vertex_exists?(v) && vertex_exists?(u)
@representation[v] << u
@weights[[v,u]] = w
else
raise ArgumentError, "One of the vertices referenced does not exist in the graph"
end
end
|
#adjust_weight(v, u, w) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 32
def adjust_weight(v, u, w)
if edge_exists?(v, u)
@weights[[v,u]] = w
else
raise ArgumentError, "That edge does not exist in the graph"
end
end
|
#edge_weight(v, u) ⇒ Object
24
25
26
27
28
29
30
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 24
def edge_weight(v, u)
if edge_exists?(v,u)
@weights[[v,u]]
else
raise ArgumentError, "That edge does not exist in the graph"
end
end
|
#neighbors_of_vertex(v) ⇒ Object
40
41
42
43
44
45
46
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 40
def neighbors_of_vertex(v)
if vertex_exists?(v)
@representation[v]
else
raise ArgumentError, "That vertex does not exist in the graph"
end
end
|
#remove_edge(v, u) ⇒ Object
15
16
17
18
19
20
21
22
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 15
def remove_edge(v, u)
if edge_exists?(v, u)
@representation[v].delete(u)
@weights.delete([v,u])
else
raise ArgumentError, "That edge does not exist in the graph"
end
end
|
#shortest_path(v, u) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 56
def shortest_path(v, u)
if vertex_exists?(u)
[].tap do |s|
previous = single_source_shortest_path_previous(v)
while previous[u]
s.insert(0, u)
u = previous[u]
end
s.insert(0, v)
end
else
raise ArgumentError, "A specified vertex does not exist in the graph"
end
end
|
#shortest_path_distance(v, u) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/graphunk/weighted_directed_graph.rb', line 48
def shortest_path_distance(v, u)
if vertex_exists?(u)
single_source_shortest_path_distances(v)[u]
else
raise ArgumentError, "A specified vertex does not exist in the graph"
end
end
|