Skip to content

Commit 799e885

Browse files
committed
added weighted graphs code
1 parent 1a61a91 commit 799e885

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
*Mykel J. Kochenderfer, Sydney M. Katz, Anthony L. Corso, and Robert J. Moss*
44

5-
All typeset code blocks from the book, [Algorithms for Validation](https://algorithmsbook.com/validation).
5+
All typeset code blocks from the book, [Algorithms for Validation](https://algorithmsbook.com/validation). We also include the [code for the weighted graphs](weighted_graphs.jl) used in the book.
66

77
We share this content in the hopes that it helps you and makes the validation algorithms
88
more approachable and accessible. Thank you for reading!

weighted_graphs.jl

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
"""
2+
Modification of the SimpleWeightedGraphs.jl package to allow for weighted graphs with abritrary states.
3+
Used in the Algorithms for Validation book by Mykel J. Kochenderfer, Sydney M. Katz, Anthony L. Corso, and Robert J. Moss
4+
"""
5+
6+
@with_kw struct WeightedGraph{T}
7+
g::SimpleWeightedDiGraph
8+
states2ind::Dict{T,Int} = Dict()
9+
ind2states::Dict{Int,T} = Dict()
10+
end
11+
12+
function WeightedGraph{T}(nv::Int) where T
13+
return WeightedGraph{T}(SimpleWeightedDiGraph(nv), Dict(), Dict())
14+
end
15+
16+
function WeightedGraph(vertices::Vector)
17+
nv = length(vertices)
18+
T = typeof(first(vertices))
19+
return WeightedGraph{T}(nv)
20+
end
21+
22+
function SimpleWeightedGraphs.add_edge!(g::WeightedGraph{T}, si::T, sj::T, w::Real) where T
23+
val = values(g.states2ind)
24+
n = isempty(val) ? 0 : maximum(val)
25+
26+
if !haskey(g.states2ind, si)
27+
n += 1
28+
g.states2ind[si] = n
29+
end
30+
i = g.states2ind[si]
31+
g.ind2states[i] = si
32+
33+
if !haskey(g.states2ind, sj)
34+
n += 1
35+
g.states2ind[sj] = n
36+
end
37+
j = g.states2ind[sj]
38+
g.ind2states[j] = sj
39+
40+
return add_edge!(g.g, i, j, w)
41+
end
42+
43+
function SimpleWeightedGraphs.outneighbors(g::WeightedGraph{T}, si::T) where T
44+
i = g.states2ind[si]
45+
J = outneighbors(g.g, i)
46+
return map(j->g.ind2states[j], J)
47+
end
48+
49+
function SimpleWeightedGraphs.inneighbors(g::WeightedGraph{T}, si::T) where T
50+
i = g.states2ind[si]
51+
J = inneighbors(g.g, i)
52+
return map(j->g.ind2states[j], J)
53+
end
54+
55+
function SimpleWeightedGraphs.edges(g::WeightedGraph{T}) where T
56+
E = []
57+
for edge in edges(g.g)
58+
(i,j,w) = Tuple(edge)
59+
si = g.ind2states[i]
60+
sj = g.ind2states[j]
61+
push!(E, (si, sj, w))
62+
end
63+
return E
64+
end
65+
66+
function SimpleWeightedGraphs.get_weight(g::WeightedGraph{T}, si::T, sj::T) where T
67+
i = g.states2ind[si]
68+
j = g.states2ind[sj]
69+
return get_weight(g.g, i, j)
70+
end
71+
72+
function to_matrix(g::WeightedGraph)
73+
𝒮 = keys(g.states2ind)
74+
n = length(𝒮)
75+
T = zeros(n, n)
76+
for s in 𝒮
77+
for s′ in outneighbors(g, s)
78+
T[g.states2ind[s], g.states2ind[s′]] = get_weight(g, s, s′)
79+
end
80+
end
81+
return T
82+
end
83+
84+
index(g::WeightedGraph, s) = g.states2ind[s]
85+
state(g::WeightedGraph, i) = g.ind2states[i]

0 commit comments

Comments
 (0)