From 08498fea68d07e02db83dde4d66f4dc716d62a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Fri, 17 Dec 2021 22:50:48 +0100 Subject: [PATCH] feat: ajout de bellman ford --- tp4.ipynb | 417 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 408 insertions(+), 9 deletions(-) diff --git a/tp4.ipynb b/tp4.ipynb index 397fdd1..162ebaf 100644 --- a/tp4.ipynb +++ b/tp4.ipynb @@ -9,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 1, "metadata": {}, "outputs": [ { @@ -61,7 +61,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 2, "metadata": {}, "outputs": [ { @@ -103,39 +103,438 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "3906\n", - "Int8[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]\n" + "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(\"test4.opb\")\n", + "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": 126, + "metadata": {}, + "outputs": [ + { + "data": { + "image/svg+xml": "\n\n\n \n \n \n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n \n \n 9.0\n \n \n \n \n 3.0\n \n \n \n \n 6.0\n \n \n \n \n 2.0\n \n \n \n \n 2.0\n \n \n \n \n 1.0\n \n \n \n \n 2.0\n \n \n \n \n 2.0\n \n \n\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n\n \n \n 1\n \n \n \n \n 2\n \n \n \n \n 3\n \n \n \n \n 4\n \n \n \n \n 5\n \n \n\n\n", + "text/html": [ + "\n", + "\n", + "\n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " 9.0\n", + " \n", + " \n", + " \n", + " \n", + " 3.0\n", + " \n", + " \n", + " \n", + " \n", + " 6.0\n", + " \n", + " \n", + " \n", + " \n", + " 2.0\n", + " \n", + " \n", + " \n", + " \n", + " 2.0\n", + " \n", + " \n", + " \n", + " \n", + " 1.0\n", + " \n", + " \n", + " \n", + " \n", + " 2.0\n", + " \n", + " \n", + " \n", + " \n", + " 2.0\n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + " \n", + " \n", + " 1\n", + " \n", + " \n", + " \n", + " \n", + " 2\n", + " \n", + " \n", + " \n", + " \n", + " 3\n", + " \n", + " \n", + " \n", + " \n", + " 4\n", + " \n", + " \n", + " \n", + " \n", + " 5\n", + " \n", + " \n", + "\n", + "\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.8906990112018535cx, 0.8129590369614906cy), (0.13087368362935287cx, 0.9764778009585108cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.9440455894018296cx, 0.6926427062042235cy), (0.3845562161328376cx, -0.27520393020637507cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.04598844997483381cx, 0.8908951380516595cy), (0.3041860503910396cx, -0.26289319997381244cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.07735325117145776cx, 0.9479072249973377cy), (-0.901074053997336cx, 0.5141494073519517cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.3041860503910396cx, -0.26289319997381244cy), (0.045988449974833784cx, 0.8908951380516595cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.23388272856129344cx, -0.43139752704863454cy), (-0.5780983017482865cx, -0.9406005348735184cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.5780983017482865cx, -0.9406005348735184cy), (0.2338827285612934cx, -0.43139752704863454cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.6972331325527618cx, -0.8908951377579393cy), (-0.9755842461688985cx, 0.35295177010722867cy)])], Symbol(\"\"))]), List([Compose.Property{Compose.LineWidthPrimitive}(Compose.LineWidthPrimitive[Compose.LineWidthPrimitive(1.3416407864998738mm)]), 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.22993542917571522cx, 0.9901442038981201cy), (0.13087368362935287cx, 0.9764778009585108cy), (0.21554397780937445cx, 0.9232712103567009cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.40197465254261655cx, -0.17673262437041853cy), (0.3845562161328376cx, -0.27520393020637507cy), (0.46119564785337674cx, -0.21096688866839003cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.2502884354107841cx, -0.17866109742709843cy), (0.3041860503910396cx, -0.26289319997381244cy), (0.3170414337929225cx, -0.16372294678471044cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.8338639299345193cx, 0.588195333799399cy), (-0.901074053997336cx, 0.5141494073519517cy), (-0.8019923078694982cx, 0.5276700428495417cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.09988606495508923cx, 0.8066630355049456cy), (0.045988449974833784cx, 0.8908951380516595cy), (0.03313306657295091cx, 0.7917248848625575cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.5166591999010254cx, -0.8617003652653238cy), (-0.5780983017482865cx, -0.9406005348735184cy), (-0.4803171677616572cx, -0.9196517918017049cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(0.1724436267140323cx, -0.5102976966568291cy), (0.2338827285612934cx, -0.43139752704863454cy), (0.13610159457466414cx, -0.4523462701204481cy)]), Compose.LinePrimitive{Tuple{Measures.Measure, Measures.Measure}}(Tuple{Measures.Measure, Measures.Measure}[(-0.921686632201951cx, 0.26871966691212906cy), (-0.9755842461688985cx, 0.35295177010722867cy), (-0.9884396307637945cx, 0.2537815170727763cy)])], Symbol(\"\"))]), List([Compose.Property{Compose.LineWidthPrimitive}(Compose.LineWidthPrimitive[Compose.LineWidthPrimitive(1.3416407864998738mm)]), 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.2661795211234047cx, 0.9473592094800003cy), \"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)), 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.49645135415100033cx, -0.08163933696161432cy), \"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.2518445278588019cx, -0.028998546441614675cy), \"6.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.7446068262921984cx, 0.596542474261967cy), \"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.09832997250707148cx, 0.6570004845194618cy), \"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.4224625826575783cx, -0.8429995154805382cy), \"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.0782470094705853cx, -0.5289985464416147cy), \"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.918204344680415cx, 0.09654247426196705cy), \"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))], 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, 0.7894368379200014cy), 0.044721359549995794w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.021572694831206274cx, 1.0cy), 0.044721359549995794w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((0.3286018055346671cx, -0.3719980619221529cy), 0.044721359549995794w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-0.6728173787216601cx, -1.0cy), 0.044721359549995794w), Compose.CirclePrimitive{Tuple{Measures.Measure, Measures.Measure}, Measures.Measure}((-1.0cx, 0.4620566323492894cy), 0.044721359549995794w)], 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, 0.7894368379200014cy), \"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.021572694831206274cx, 1.0cy), \"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.3286018055346671cx, -0.3719980619221529cy), \"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.6728173787216601cx, -1.0cy), \"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}}((-1.0cx, 0.4620566323492894cy), \"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))], 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(5)\n", + "\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": 136, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "([0.0, 5.0, 3.0, 4.0, 6.0], [0, 3, 1, 3, 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.6.4", + "display_name": "Julia 1.7.0", "language": "julia", - "name": "julia-1.6" + "name": "julia-1.7" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.6.4" + "version": "1.7.0" }, "orig_nbformat": 4 },