{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TP4 : Programmation dynamique"
]
},
{
"cell_type": "code",
"execution_count": 33,
"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": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"analyseRecursive (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"function analyseRecursive(price, weight, i, j)\n",
" if i > 0\n",
" C1, sol1 = analyseRecursive(price, weight, i - 1, j)\n",
" if j - weight[i] >= 0\n",
" C2, sol2 = analyseRecursive(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{Int}()\n",
" end\n",
" \n",
" return res, sol\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"knapsack (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# nice read : https://jarednielsen.com/dynamic-programming-memoization-tabulation/\n",
"\n",
"function analyseDynamique(prices, weights, capacity)\n",
" n = length(prices)\n",
" memo = zeros(UInt, n, capacity)\n",
"\n",
" for i in 2:n\n",
" for j in 1:capacity\n",
" if weights[i] > j\n",
" memo[i, j] = memo[i-1, j]\n",
" else\n",
" memo[i, j] = max(memo[i-1, j], memo[i-1, j-weights[i]+1] + prices[i])\n",
" end\n",
" end\n",
" end\n",
"\n",
" return memo\n",
"end\n",
"\n",
"function knapsack(memo, weights, i, j)\n",
" if i == 1\n",
" return Vector{UInt}()\n",
" end\n",
" if memo[i, j] > memo[i-1, j]\n",
" return push!(knapsack(memo, weights, i-1, j-weights[i]+1) ,i)\n",
" else\n",
" return knapsack(memo, weights, i-1, j)\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution récursive:\n",
" 0.000466 seconds (2.16 k allocations: 78.172 KiB)\n",
" * res = 2291\n",
" * sol = [0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0]\n",
"\n",
"Solution dynamique:\n",
" 0.005790 seconds (31.52 k allocations: 647.844 KiB)\n",
" 0.000022 seconds (9 allocations: 256 bytes)\n",
" * res = 2291\n",
" * sol = UInt64[0x0000000000000003, 0x0000000000000006, 0x000000000000000f, 0x0000000000000011]\n"
]
}
],
"source": [
"prices, weights, capacity = readKnapInstance(\"test2.opb\")\n",
"\n",
"println(\"Solution récursive:\");\n",
"@time res, sol = analyseRecursive(prices, weights, length(prices), capacity)\n",
"println(\" * res = \", res);\n",
"println(\" * sol = \", sol);\n",
"\n",
"println()\n",
"\n",
"println(\"Solution dynamique:\")\n",
"@time memo = analyseDynamique(prices, weights, capacity)\n",
"@time objects_taken = knapsack(memo, weights, length(prices), capacity)\n",
"objects_taken_price = sum(prices[i] for i in objects_taken)\n",
"println(\" * res = \", objects_taken_price)\n",
"println(\" * sol = \", objects_taken)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Rapport\n",
"\n",
"Voici deux implémentation de l'algorithme du sac à dos 1-0.\n",
"- La première implémentation est naïve et récursive.\n",
"- La deuxième implémentation est dynamique.\n",
"\n",
"La programmation dynamique consiste à sauvegarder des résultats passés d'un calcul, pour ne pas avoir à les recalculer entièrement lorsque l'on retombe dessus. Dans notre cas, nous stockons les résultats précédents dans une matrice `mémo` de taille $n \\times \\text{capacity}$.\n",
"\n",
"À l'issue de benchmarks sur les deux implémentation, on remarque que l'algorithme récursif est plus rapide sur de petits jeux de données, et ce à cause de l'overhead causé par l'allocation de la matrice `mémo`. Cependant sur des exemples plus grands (comme `test4.opb`) on observe tout l'intéret de la programmation dynamique. En effet, alors que l'algorithme récursif peine à finir, l'algorithme dynamique est très rapide. Ce gain de vitesse est dû au fait que l'on a échangé de la complexité temporelle par de la complexité spatiale.\n",
"\n",
"Enfin, une propriété non négligeable de la programmation dynamique est que celle-ci permet le calcul parallel (via la matrice `memo`), ainsi (sur des jeux de données encore plus grand) la vitesse de l'algorithme n'est limité que par le nombre de ressources disponibles (contrairement à l'algorithme récursive et à celui du TP2-3)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BONUS : Bellman Ford"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General.toml`\n",
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Manifest.toml`\n",
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
" 1 dependency successfully precompiled in 4 seconds (265 already precompiled, 2 skipped during auto due to previous errors)\n",
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Manifest.toml`\n",
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
" 1 dependency successfully precompiled in 3 seconds (265 already precompiled, 2 skipped during auto due to previous errors)\n",
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.7/Manifest.toml`\n",
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
" 1 dependency successfully precompiled in 2 seconds (265 already precompiled, 2 skipped during auto due to previous errors)\n",
"WARNING: using Graphs.weights in module Main conflicts with an existing identifier.\n"
]
}
],
"source": [
"using Pkg\n",
"Pkg.add(\"Graphs\")\n",
"Pkg.add(\"GraphPlot\")\n",
"Pkg.add(\"SimpleWeightedGraphs\")\n",
"using Graphs\n",
"using GraphPlot\n",
"using SimpleWeightedGraphs"
]
},
{
"cell_type": "code",
"execution_count": 38,
"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.9802711117800587cx, 0.8998629057927496cy), (0.784974686436027cx, -0.09139500620898157cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.9071999762484526cx, 0.9575168002808296cy), (0.4734194836513963cx, 0.7589351344978663cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.7079653669660091cx, -0.2760046927907526cy), (0.2743082970191166cx, -0.9155274076254794cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.1694484274597039cx, -0.909706790306055cy), (-0.23432630192287096cx, -0.1434489780085348cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.38396767439513146cx, -0.05298765647806152cy), (-0.8979380658370757cx, -0.05214106752072823cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.420428993171646cx, 0.622473917522806cy), (0.7254362649442886cx, -0.09755408316034196cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.31403270888756757cx, 0.6391029124429539cy), (-0.21531898921992582cx, 0.024193254021152327cy)])], 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.769582276470473cx, 0.007413261235864943cy), (0.784974686436027cx, -0.09139500620898157cy), (0.8366961463638463cx, -0.005809431589569342cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.5446245705250035cx, 0.8291479225305464cy), (0.4734194836513963cx, 0.7589351344978663cy), (0.5730976549616812cx, 0.7669515030171833cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.29873918478281114cx, -0.8185576611935238cy), (0.2743082970191166cx, -0.9155274076254794cy), (0.3553543943518648cx, -0.8569481441687689cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.16026142976834024cx, -0.21063822352247863cy), (-0.23432630192287096cx, -0.1434489780085348cy), (-0.22077773269912518cx, -0.2425269083057981cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.8039125952868633cx, -0.018093881325520436cy), (-0.8979380658370757cx, -0.05214106752072823cy), (-0.8040252671794682cx, -0.08649781719667524cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.6572903829324035cx, -0.02436843726702323cy), (0.7254362649442886cx, -0.09755408316034196cy), (0.7202763170947744cx, 0.002312702800708638cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.1799325288656719cx, 0.1177229200293062cy), (-0.21531898921992582cx, 0.024193254021152327cy), (-0.12809167745573796cx, 0.07309515666709421cy)])], 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.8239343486620643cx, 0.10635092468782606cy), \"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.5354645949248866cx, 0.787338951084022cy), \"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.3540823488808015cx, -0.797883025104058cy), \"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.15717233873189534cx, -0.28986682623594223cy), \"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.8204764350580518cx, -0.05226865884179743cy), \"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.6690892136370264cx, 0.03546390838250005cy), \"-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.11627444019919314cx, 0.13924615745873173cy), \"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.7652457982160856cx, -0.19153210041623192cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.2170278657690401cx, -1.0cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-0.28190574023220716cx, -0.05315576831458968cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.3806194598998489cx, 0.716451934778696cy), 0.04082482904638631w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-1.0cx, -0.05197295568420002cy), 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.7652457982160856cx, -0.19153210041623192cy), \"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.2170278657690401cx, -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.28190574023220716cx, -0.05315576831458968cy), \"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.3806194598998489cx, 0.716451934778696cy), \"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.05197295568420002cy), \"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",
"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",
"# g = SimpleWeightedDiGraph(5)\n",
"# add_edge!(g, 1, 2, 9)\n",
"# add_edge!(g, 1, 3, 3)\n",
"# add_edge!(g, 2, 3, 6)\n",
"# add_edge!(g, 2, 5, 2)\n",
"# add_edge!(g, 3, 4, 1)\n",
"# add_edge!(g, 3, 2, 2)\n",
"# add_edge!(g, 4, 3, 2)\n",
"# add_edge!(g, 4, 5, 2)\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": 39,
"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
}