{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TP 4 : Programmation dynamique"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"readKnapInstance"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"\"\"Open and read a KnapFile.\n",
"\n",
"Args: \\\\\n",
" - filename (String): the name of the file to read.\n",
"\n",
"Returns: \\\\\n",
" - price (Vector{Integer}): prices of items to put in the KnapSack. \\\\\n",
" - weight (Vector{Integer}): weights of items to put in the KnapSack. \\\\\n",
" - capacity (Integer): the maximum capacity of the KnapSack.\n",
"\"\"\"\n",
"function readKnapInstance(filename)\n",
" price = []\n",
" weight = []\n",
" capacity = -1\n",
" open(filename) do f\n",
" for i = 1:3\n",
" tok = split(readline(f))\n",
" if (tok[1] == \"ListPrices=\")\n",
" for i = 2:(length(tok)-1)\n",
" push!(price, parse(Int64, tok[i]))\n",
" end\n",
" elseif (tok[1] == \"ListWeights=\")\n",
" for i = 2:(length(tok)-1)\n",
" push!(weight, parse(Int64, tok[i]))\n",
" end\n",
" elseif (tok[1] == \"Capacity=\")\n",
" capacity = parse(Int64, tok[2])\n",
" else\n",
" println(\"Unknown read :\", tok)\n",
" end\n",
" end\n",
" end\n",
" return price, weight, capacity\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"analyseDynamique (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"function analyseDynamique(price, weight, i, j)\n",
" if i > 0\n",
" C1, sol1 = analyseDynamique(price, weight, i - 1, j)\n",
" if j - weight[i] >= 0\n",
" C2, sol2 = analyseDynamique(price, weight, i - 1, j - weight[i])\n",
" C2 += price[i]\n",
" if C1 >= C2\n",
" res = C1\n",
" sol = push!(sol1, 0)\n",
" else\n",
" res = C2\n",
" sol = push!(sol2, 1)\n",
" end\n",
" else\n",
" res = C1\n",
" sol = push!(sol1, 0)\n",
" end\n",
" else\n",
" res = 0\n",
" sol = Vector{Int8}()\n",
" end\n",
" \n",
" return res, sol\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2291\n",
"Int8[0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0]\n"
]
}
],
"source": [
"price, weight, capacity = readKnapInstance(\"test2.opb\")\n",
"\n",
"res, sol = analyseDynamique(price, weight, length(price), capacity)\n",
"\n",
"println(res)\n",
"println(sol)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"using Pkg\n",
"Pkg.add(\"GraphPlot\")\n",
"Pkg.add(\"Graphs\")\n",
"Pkg.add(\"SimpleWeightedGraphs\")\n",
"using Graphs\n",
"using GraphPlot\n",
"using SimpleWeightedGraphs"
]
},
{
"cell_type": "code",
"execution_count": 139,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "\n\n",
"text/html": [
"\n",
"\n"
],
"text/plain": [
"Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), Compose.UnitBox{Float64, Float64, Float64, Float64}(-1.2, -1.2, 2.4, 2.4, 0.0mm, 0.0mm, 0.0mm, 0.0mm), nothing, nothing, nothing, List([Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), nothing, nothing, nothing, nothing, List([]), List([Compose.Form{Compose.LinePrimitive}(Compose.LinePrimitive[Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.9875212150831064cx, -0.8987036693968405cy), (0.877458068298961cx, -0.005267891945659575cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.9198500428646786cx, -0.936812572153372cy), (0.4606464421311852cx, -0.5747924859882558cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.8164636743552127cx, 0.18582210406558997cy), (0.4250781355901627cx, 0.9102063345919099cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.3024589870083034cx, 0.9298192897436443cy), (-0.18176538635950845cx, 0.4712283688448219cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.3512529372822731cx, 0.437359599352651cy), (-0.9046159886322398cx, 0.6480205482221203cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.4441240219204178cx, -0.4318040355192253cy), (0.8013517464575136cx, 0.01622741603509739cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.3221212174480839cx, -0.4278853185016135cy), (-0.19749365836673302cx, 0.3173279189484518cy)])], Symbol(\"\"))]), List([Compose.Property{Compose.LineWidthPrimitive}(Compose.LineWidthPrimitive[Compose.LineWidthPrimitive(1.2247448713915892mm)]), Compose.Property{Compose.FillPrimitive}(Compose.FillPrimitive[Compose.FillPrimitive(RGBA{Float64}(0.0,0.0,0.0,0.0))]), Compose.Property{Compose.StrokePrimitive}(Compose.StrokePrimitive[Compose.StrokePrimitive(RGBA{Float64}(0.8274509803921568,0.8274509803921568,0.8274509803921568,1.0))])]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\")), Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), nothing, nothing, nothing, nothing, List([]), List([Compose.Form{Compose.LinePrimitive}(Compose.LinePrimitive[Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.9228927791817403cx, -0.09435036523304792cy), (0.877458068298961cx, -0.005267891945659575cy), (0.855001966026066cx, -0.10271389479189891cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.5556158013853003cx, -0.6061105447222006cy), (0.4606464421311852cx, -0.5747924859882558cy), (0.513266331940755cx, -0.6598286382790829cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.4998375444012923cx, 0.8437907430925459cy), (0.4250781355901627cx, 0.9102063345919099cy), (0.43965604597499464cx, 0.8112746180960773cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.13705599378712974cx, 0.5606770704329888cy), (-0.18176538635950845cx, 0.4712283688448219cy), (-0.09001948754079048cx, 0.5110114060585084cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.8046267720778828cx, 0.6465520207808045cy), (-0.9046159886322398cx, 0.6480205482221203cy), (-0.8289637561627169cx, 0.5826237614327278cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.7695115448047268cx, -0.07856816170305383cy), (0.8013517464575136cx, 0.01622741603509739cy), (0.7160273140904478cx, -0.035923722411134236cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.11569182604841012cx, 0.2598087027736378cy), (-0.19749365836673302cx, 0.3173279189484518cy), (-0.17180245875838335cx, 0.22068443876130242cy)])], Symbol(\"\"))]), List([Compose.Property{Compose.LineWidthPrimitive}(Compose.LineWidthPrimitive[Compose.LineWidthPrimitive(1.2247448713915892mm)]), Compose.Property{Compose.StrokePrimitive}(Compose.StrokePrimitive[Compose.StrokePrimitive(RGBA{Float64}(0.8274509803921568,0.8274509803921568,0.8274509803921568,1.0))])]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\")), Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), nothing, nothing, nothing, nothing, List([]), List([Compose.Form{Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}}(Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}[Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.8987344625365506cx, -0.1779786710068751cy), \"3.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.5353723637468979cx, -0.6337037936062209cy), \"5.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.4986667157679978cx, 0.774007109664375cy), \"4.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((-0.09776106279505772cx, 0.5507857439413496cy), \"2.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((-0.8139672314786283cx, 0.6135112813868454cy), \"3.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.7438585837855166cx, -0.055879935542282044cy), \"-1.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((-0.09677757318691874cx, 0.17288447940594265cy), \"9.0\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm))], Symbol(\"\"))]), List([Compose.Property{Compose.FontSizePrimitive}(Compose.FontSizePrimitive[Compose.FontSizePrimitive(4.0mm)]), Compose.Property{Compose.StrokePrimitive}(Compose.StrokePrimitive[Compose.StrokePrimitive(RGBA{Float64}(0.0,0.0,0.0,0.0))]), Compose.Property{Compose.FillPrimitive}(Compose.FillPrimitive[Compose.FillPrimitive(RGBA{Float64}(0.0,0.0,0.0,1.0))])]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\")), Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), nothing, nothing, nothing, nothing, List([]), List([Compose.Form{Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}}(Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}[Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((1.0cx, -1.0cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.8649792833820675cx, 0.09602843865749988cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.3765625265633079cx, 1.0cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-0.25586892591451293cx, 0.40104765858846614cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.38049648499586386cx, -0.5116050581416278cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-1.0cx, 0.6843324889863052cy), 0.04082482904638631w)], Symbol(\"\"))]), List([Compose.Property{Compose.LineWidthPrimitive}(Compose.LineWidthPrimitive[Compose.LineWidthPrimitive(0.0mm)]), Compose.Property{Compose.StrokePrimitive}(Compose.StrokePrimitive[Compose.StrokePrimitive(RGBA{Float64}(0.0,0.0,0.0,0.0))]), Compose.Property{Compose.FillPrimitive}(Compose.FillPrimitive[Compose.FillPrimitive(RGBA{Float64}(0.25098039215686274,0.8784313725490196,0.8156862745098039,1.0))])]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\")), Compose.Context(Measures.BoundingBox{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}, Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}((0.0w, 0.0h), (1.0w, 1.0h)), nothing, nothing, nothing, nothing, List([]), List([Compose.Form{Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}}(Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}[Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((1.0cx, -1.0cy), \"1\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.8649792833820675cx, 0.09602843865749988cy), \"2\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.3765625265633079cx, 1.0cy), \"3\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((-0.25586892591451293cx, 0.40104765858846614cy), \"4\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((0.38049648499586386cx, -0.5116050581416278cy), \"5\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm)), Compose.TextPrimitive{Tuple{Measures.Length{:cx, Float64}, Measures.Length{:cy, Float64}}, Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}, Tuple{Measures.AbsoluteLength, Measures.AbsoluteLength}}((-1.0cx, 0.6843324889863052cy), \"6\", Compose.HCenter(), Compose.VCenter(), Compose.Rotation{Tuple{Measures.Length{:w, Float64}, Measures.Length{:h, Float64}}}(0.0, (0.5w, 0.5h)), (0.0mm, 0.0mm))], Symbol(\"\"))]), List([Compose.Property{Compose.FontSizePrimitive}(Compose.FontSizePrimitive[Compose.FontSizePrimitive(4.0mm)]), Compose.Property{Compose.StrokePrimitive}(Compose.StrokePrimitive[Compose.StrokePrimitive(RGBA{Float64}(0.0,0.0,0.0,0.0))]), Compose.Property{Compose.FillPrimitive}(Compose.FillPrimitive[Compose.FillPrimitive(RGBA{Float64}(0.0,0.0,0.0,1.0))])]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\"))]), List([]), List([]), 0, false, false, false, false, nothing, nothing, 0.0, Symbol(\"\"))"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"g = SimpleWeightedDiGraph(6)\n",
"\n",
"add_edge!(g, 1, 2, 3)\n",
"add_edge!(g, 1, 5, 5)\n",
"add_edge!(g, 2, 3, 4)\n",
"add_edge!(g, 3, 4, 2)\n",
"add_edge!(g, 4, 6, 3)\n",
"add_edge!(g, 5, 2, -1)\n",
"add_edge!(g, 5, 4, 9)\n",
"\n",
"nodelabel = 1:nv(g)\n",
"edgelabel = edges(g).iter.is[3]\n",
"gplot(g, nodelabel=nodelabel, edgelabel=edgelabel)"
]
},
{
"cell_type": "code",
"execution_count": 140,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"([0.0, 3.0, 7.0, 9.0, 5.0, 12.0], [0, 1, 2, 3, 1, 4])"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# https://en.wikipedia.org/wiki/Bellman%E2%80%93Ford_algorithm\n",
"function bellmanFord(graph, source)\n",
"\n",
" # Step 1: initialize graph\n",
" d = fill(Inf, nv(graph)); # distance array\n",
" p = fill(0, nv(graph)); # predecessor array\n",
" d[source] = 0; # source's distance to source is null\n",
"\n",
" # Step 2: relax edges repeatedly\n",
" for _ in 2:nv(graph)\n",
" # println(\"test: \", k)\n",
" for (v, u, w) in edges(graph).iter\n",
" # println(u, \" \", v, \" \", w)\n",
" if d[u] + w < d[v]\n",
" d[v] = d[u] + w\n",
" p[v] = u\n",
" end\n",
" end\n",
" end\n",
"\n",
" # Step 3: check for negative-weight cycles\n",
" for (v, u, w) in edges(graph).iter\n",
" if d[u] + w < d[v]\n",
" error(\"Graph contains a negative-weight cycle\")\n",
" end\n",
" end\n",
"\n",
" return d, p\n",
"end\n",
"bellmanFord(g, 1)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.0",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}