Graph algorithms
Author
Vojtěch (Vojtech) Bartík (Bartik)
Title
Graph algorithms
Description
Defines functions realizing some well known algorithms for simple graphs (or their modifications) and producing not only expected results but also tables (and in some cases also animations) helping to understand how these algorithms work.
Category
Working Material
Keywords
URL
http://www.notebookarchive.org/2021-11-a6bkcrc/
DOI
https://notebookarchive.org/2021-11-a6bkcrc
Date Added
2021-11-22
Date Last Modified
2021-11-22
File Size
0.54 megabytes
Supplements
Rights
Redistribution rights reserved



Graph Algorithms
Graph Algorithms
Demo
Vojtěch Bartík, February 2021
Convention : All graphs are supposed simple without loops. Vertices are positive natural numbers and all edges are supposed to be directed or undirected.
In[]:=
Off[EdgeWeightedGraphQ::argx,General::argx,Graph::supp,Unset::norep];
Axiliary Definitions
Axiliary Definitions
EdgeListQ[E] returns True if E may be considered as the EdgeList of a directed or undirected graph in the sense of Mathematica, and False in the opposite case.EdgeListQ[E,∞] returns True if Graph[E] is undirected graph in the sense of Mathematica and False in the opposite case.
EdgeListQ[E]
E
EdgeListQ[E,∞]
Graph[E]
WeightedEdgeListQ[E] returns True if First/@E may be considered as the EdgeList of a directed or undirected graph with the EdgeWeightLast/@E in the sense of Mathematica, and False in the opposite case.WeightedEdgeListQ[E,∞] returns True if Graph[First/@E,EdgeWeight->Last/@E] is the undirected graph in the sense of Mathematica, and False in the opposite case.
WeightedEdgeListQ[E]
First/@E
EdgeWeightLast/@E
WeightedEdgeListQ[E,∞]
Graph[First/@E,EdgeWeight->Last/@E]
WeightedAdjacencyMatrixQ[M] returns True if M is a square matrix with numeric entries and zeros on the diagonal, and False in the opposite case.WeightedAdjacencyMatrixQ[M,∞] returns True if M is a symmetric square matrix with numeric non-diagonal entries and ∞’s on the diagonal, and False in the opposite case.
WeightedAdjacencyMatrixQ[M]
M
WeightedAdjacencyMatrixQ[M,∞]
M
∞
ToEdgeList
ToEdgeList
ToWeightedEdgeList
ToWeightedEdgeList
ToWeightedAdjacencyMatrix
ToWeightedAdjacencyMatrix
ToGraph
ToGraph
ToEdgeWeightedGraph
ToEdgeWeightedGraph
Random EdgeLists, Matrices and Graphs
Random EdgeLists, Matrices and Graphs
RandomEdgeList, RandomWeightedEdgeList
RandomEdgeList, RandomWeightedEdgeList
RandomWeightedAdjacencyMatrix
RandomWeightedAdjacencyMatrix
RandomAcyclicGraph
RandomAcyclicGraph
Components
Components
Example 1
Example 1
In[]:=
V8=Range[1,10];E8={{2,3},{3,4},{4,3},{5,9},{6,8},{7,8},{7,10},{8,10}};gE8=ToGraph[E8,EdgeShapeFunctionGraphElementData[{"Arrow","ArrowSize".05}],DirectedEdgesTrue,VertexLabels"Name",VertexSize0.1,VertexStyleRed,ImagePadding20,GraphLayoutAutomatic,VertexCoordinatesEllipse[6,3,10]]
Out[]=
In[]:=
{AdjacentVertices[gE8,#,-1]&/@V8,AdjacentVertices[gE8,#,1]&/@V8,AdjacentVertices[E8,#,0]&/@V8,AdjacentVertices[E8,#]&/@V8}//Column[#,Center,Spacings1]&
Out[]=
{{},{},{2,4},{3},{},{},{},{6,7},{5},{7,8}} |
{{},{3},{4},{3},{9},{8},{8,10},{10},{},{}} |
{{},{3},{2,4},{3},{9},{8},{8,10},{6,7,10},{5},{7,8}} |
{{},{3},{2,4},{3},{9},{8},{8,10},{6,7,10},{5},{7,8}} |
In[]:=
{AccesibleVertices[gE8,#,-1]&/@V8,AccesibleVertices[gE8,#,1]&/@V8,AccesibleVertices[E8,#,0]&/@V8,AccesibleVertices[E8,#]&/@V8}//Column[#,Center,Spacings1]&
Out[]=
{{1},{2},{2,3,4},{2,3,4},{5},{6},{7},{6,7,8},{5,9},{6,7,8,10}} |
{{1},{2,3,4},{3,4},{3,4},{5,9},{6,8,10},{7,8,10},{8,10},{9},{10}} |
{{1},{2,3,4},{2,3,4},{2,3,4},{5,9},{6,7,8,10},{6,7,8,10},{6,7,8,10},{5,9},{6,7,8,10}} |
{{1},{2,3,4},{2,3,4},{2,3,4},{5,9},{6,7,8,10},{6,7,8,10},{6,7,8,10},{5,9},{6,7,8,10}} |
In[]:=
{Components@E8,Components@gE8,StrongComponents@E8//Sort,ConnectedComponents@gE8//Sort}//Column[#,Center]&
Out[]=
{{1},{2,3,4},{5,9},{6,7,8,10}} |
{{1},{2,3,4},{5,9},{6,7,8,10}} |
{{1},{2},{5},{6},{7},{8},{9},{10},{3,4}} |
{{1},{2},{5},{6},{7},{8},{9},{10},{3,4}} |
Example 2
Example 2
In[]:=
V9=Range[13];E9={{1,3},{1,11},{2,6},{3,9},{4,12},{5,7},{6,10},{7,13},{9,11}};gE9∞=ToGraph[E9,∞,ImagePadding20,GraphLayoutAutomatic,VertexLabels"Name",VertexSize0.15,VertexStyleRed,VertexCoordinatesEllipse[6,3,13]]
Out[]=
In[]:=
{AdjacentVertices[gE9∞,#,-1]&/@V9,AdjacentVertices[gE9∞,#,1]&/@V9,AdjacentVertices[E9,#,0]&/@V9,AdjacentVertices[E9,#]&/@V9}//Column[#,Center,Spacings1]&
Out[]=
{{},{},{1},{},{},{2},{5},{},{3},{6},{1,9},{4},{7}} |
{{3,11},{6},{9},{12},{7},{10},{13},{},{11},{},{},{},{}} |
{{3,11},{6},{1,9},{12},{7},{2,10},{5,13},{},{3,11},{6},{1,9},{4},{7}} |
{{3,11},{6},{1,9},{12},{7},{2,10},{5,13},{},{3,11},{6},{1,9},{4},{7}} |
In[]:=
{AccesibleVertices[gE9∞,#,-1]&/@V9,AccesibleVertices[gE9∞,#,1]&/@V9,AccesibleVertices[gE9∞,#,0]&/@V9,AccesibleVertices[gE9∞,#]&/@V9}//Column[#,Center,Spacings1]&
Out[]=
{{1},{2},{1,3},{4},{5},{2,6},{5,7},{8},{1,3,9},{2,6,10},{1,3,9,11},{4,12},{5,7,13}} |
{{1,3,9,11},{2,6,10},{3,9,11},{4,12},{5,7,13},{6,10},{7,13},{8},{9,11},{10},{11},{12},{13}} |
{{1,3,9,11},{2,6,10},{1,3,9,11},{4,12},{5,7,13},{2,6,10},{5,7,13},{8},{1,3,9,11},{2,6,10},{1,3,9,11},{4,12},{5,7,13}} |
{{1,3,9,11},{2,6,10},{1,3,9,11},{4,12},{5,7,13},{2,6,10},{5,7,13},{8},{1,3,9,11},{2,6,10},{1,3,9,11},{4,12},{5,7,13}} |
In[]:=
{Components@E9,Components@gE9∞,ConnectedComponents@gE9∞}//Column[#,Center]&
Out[]=
{{1,3,9,11},{2,6,10},{4,12},{5,7,13},{8}} |
{{1,3,9,11},{2,6,10},{4,12},{5,7,13},{8}} |
{{1,3,11,9},{5,7,13},{6,2,10},{12,4},{8}} |
In[]:=
{StrongComponents@E9//Sort,ConnectedComponents@ToGraph[E9]//Sort}//Column[#,Center]&
Out[]=
{{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}} |
{{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13}} |
Minimal Spanning Tree
Minimal Spanning Tree
Options
Options
Alternatives for optional arguments of Jarnik-Prim’ algorithm: -- Dividers -> {{Thin,Thin,{Dotted},Thin,Thin},Thin} | any admissible data,-- FrameColor → Red,-- GraphLayout Automatic | any Graph layout, -- ImagePadding -> 10 | any number,-- ImageSize -> Automatic | {450, 150} | any size,-- ItemColor → Green | any color,-- SelectionRule -> First | Last | RandomChoice,-- ShowTree -> True | False,-- Sort -> False | True,-- Spacings → {1, 1} | any pair of positive numbers | Automatic,-- Trace True | False,-- Tree True | False,-- TreeEdgeStyle {Thickness[0.004], Red} | any admissible Graph EdgeStyle,-- VertexCoordinates Automatic | Ellipse[a, b] ,-- VertexLabels -> “Name” | any admissible Graph VertexLabels,-- VertexLabelStyle Directive[Blue, 10, Bold] | any admissible Graph VertexLabelStyle.-- VertexSize -> Tiny | Automatic | Small | Medium | Large | number | {“Scaled”, number},-- VertexStyle Blue | any color.
In[]:=
Options[JarnikPrimTree]
Out[]=
Dividers{{Thickness[Tiny],Thickness[Tiny],{Dashing[{0,Small}]},Thickness[Tiny],Thickness[Tiny]},Thickness[Tiny]},FrameColor
,GraphLayoutAutomatic,ImagePadding10,ImageSizeAutomatic,ItemColor
,SelectionRuleFirst,ShowTreeTrue,SortFalse,Spacings{0.7,0.7},TraceTrue,TreeTrue,TreeEdgeStyleThickness[0.004],
,VertexCoordinatesAutomatic,VertexLabelsName,VertexLabelStyleDirective
,10,Bold,VertexSizeTiny,VertexStyle
Alternatives for optional arguments of Borůvka-Kruskal’s algorithm -- AdjustedEdgeList → True, -- GraphLayout Automatic | any Graph layout,-- ImagePadding -> 10 | any number,-- ImageSize -> Automatic | {450, 150} | any size,-- SelectionRule -> First | Last | RandomChoice,-- ShowTree -> True | False,-- Sort -> False | True,-- Spacings → {1, 1} | any pair of positive numbers | Automatic,-- Trace True | False,-- Tree True | False,-- TreeEdgeStyle {Thickness[0.004], Red} | any admissible Graph EdgeStyle,-- VertexCoordinates Automatic | Ellipse[a, b],-- VertexLabels -> “Name” | any admissible Graph VertexLabels,-- VertexLabelStyle Directive[Blue, 10, Bold] | any admissible Graph VertexLabelStyle,-- VertexSize -> Tiny | Automatic | Small | Medium | Large | number | {“Scaled”, number},-- VertexStyle Blue | any color.
In[]:=
Options[BoruvkaKruskalTree]
Out[]=
AdjustedEdgeListTrue,GraphLayoutAutomatic,ImagePadding10,ImageSizeAutomatic,SelectionRuleFirst,ShowTreeTrue,SortFalse,Spacings{0.7,0.7},TraceTrue,TreeTrue,TreeEdgeStyleThickness[0.004],
,VertexCoordinatesAutomatic,VertexLabelsName,VertexLabelStyleDirective
,10,Bold,VertexSizeTiny,VertexStyle
Example 3
Example 3
In[]:=
M1=
;
∞ | 8 | 2 | 2 | 9 | 2 |
8 | ∞ | -5 | -1 | ∞ | 19 |
2 | -5 | ∞ | -5 | 3 | 14 |
2 | -1 | -5 | ∞ | 3 | -2 |
9 | ∞ | 3 | 3 | ∞ | 2 |
2 | 19 | 14 | -2 | 2 | ∞ |
In[]:=
EM1=ToWeightedEdgeList[M1];gM1=ToEdgeWeightedGraph[M1];Column[{EM1,gM1,EdgeList[gM1],Options[gM1,EdgeWeight]},Center,Spacings1]
Out[]=
{{{1,2},8},{{1,3},2},{{1,4},2},{{1,5},9},{{1,6},2},{{2,3},-5},{{2,4},-1},{{2,6},19},{{3,4},-5},{{3,5},3},{{3,6},14},{{4,5},3},{{4,6},-2},{{5,6},2}} | |||||
Graph
| |||||
{12,13,14,15,16,23,24,26,34,35,36,45,46,56} | |||||
{EdgeWeight{8,2,2,9,2,-5,-1,19,-5,3,14,3,-2,2}} |
In[]:=
JarnikPrimTree[EM1,1]
{{{1,3},2},{{3,2},-5},{{3,4},-5},{{4,6},-2},{{6,5},2}} |
Total Weight-8 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | Selected Edge | |
1 | ★ | 8 |
| 2 | 9 | 2 | {1,3}2 | |
3 | ★ |
| ★ | -5 | 3 | 2 | {3,2}-5 | |
2 | ★ | ★ | ★ |
| 3 | 2 | {3,4}-5 | |
4 | ★ | ★ | ★ | ★ | 3 |
| {4,6}-2 | |
6 | ★ | ★ | ★ | ★ |
| ★ | {6,5}2 |
In[]:=
JarnikPrimTree[gM1,1,ShowTreeFalse];
{{{1,3},2},{{3,2},-5},{{3,4},-5},{{4,6},-2},{{6,5},2}} |
Total Weight-8 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | Selected Edge | |
1 | ★ | 8 |
| 2 | 9 | 2 | {1,3}2 | |
3 | ★ |
| ★ | -5 | 3 | 2 | {3,2}-5 | |
2 | ★ | ★ | ★ |
| 3 | 2 | {3,4}-5 | |
4 | ★ | ★ | ★ | ★ | 3 |
| {4,6}-2 | |
6 | ★ | ★ | ★ | ★ |
| ★ | {6,5}2 |
In[]:=
JarnikPrimTree[gM1,1,ShowTreeFalse,TraceFalse];
{{{1,3},2},{{3,2},-5},{{3,4},-5},{{4,6},-2},{{6,5},2}} |
Total Weight-8 |
In[]:=
BoruvkaKruskalTree[M1];
{{{2,3},-5},{{3,4},-5},{{4,6},-2},{{1,3},2},{{5,6},2}} |
Total Weight-8 |
{{{2,3},-5},{{3,4},-5},{{4,6},-2},{{2,4},-1},{{1,3},2},{{1,4},2},{{1,6},2},{{5,6},2},{{3,5},3},{{4,5},3},{{1,2},8},{{1,5},9},{{3,6},14},{{2,6},19}}
Selected edge | Forest components |
{2,3}-5 | {{2,3}} |
{3,4}-5 | {{2,3,4}} |
{4,6}-2 | {{2,3,4,6}} |
{1,3}2 | {{1,2,3,4,6}} |
{5,6}2 | {{1,2,3,4,5,6}} |
In[]:=
BoruvkaKruskalTree[EM1,ShowTreeFalse];
{{{2,3},-5},{{3,4},-5},{{4,6},-2},{{1,3},2},{{5,6},2}} |
Total Weight-8 |
{{{2,3},-5},{{3,4},-5},{{4,6},-2},{{2,4},-1},{{1,3},2},{{1,4},2},{{1,6},2},{{5,6},2},{{3,5},3},{{4,5},3},{{1,2},8},{{1,5},9},{{3,6},14},{{2,6},19}}
Selected edge | Forest components |
{2,3}-5 | {{2,3}} |
{3,4}-5 | {{2,3,4}} |
{4,6}-2 | {{2,3,4,6}} |
{1,3}2 | {{1,2,3,4,6}} |
{5,6}2 | {{1,2,3,4,5,6}} |
In[]:=
BoruvkaKruskalTree[gM1,ShowTreeFalse,TraceFalse];
{{{2,3},-5},{{3,4},-5},{{4,6},-2},{{1,3},2},{{5,6},2}} |
Total Weight-8 |
Example 4
Example 4
In[]:=
M2=
;
∞ | 2 | 2 | 3 | 2 | -4 | 2 |
2 | ∞ | ∞ | 2 | ∞ | 3 | 1 |
2 | ∞ | ∞ | -3 | 4 | 1 | ∞ |
3 | 2 | -3 | ∞ | 1 | 2 | 4 |
2 | ∞ | 4 | 1 | ∞ | 2 | ∞ |
-4 | 3 | 1 | 2 | 2 | ∞ | 1 |
2 | 1 | ∞ | 4 | ∞ | 1 | ∞ |
In[]:=
EM2=ToWeightedEdgeList[M2];gM2=ToEdgeWeightedGraph[M2,GraphLayoutNone];EM2
Out[]=
{{{1,2},2},{{1,3},2},{{1,4},3},{{1,5},2},{{1,6},-4},{{1,7},2},{{2,4},2},{{2,6},3},{{2,7},1},{{3,4},-3},{{3,5},4},{{3,6},1},{{4,5},1},{{4,6},2},{{4,7},4},{{5,6},2},{{6,7},1}}
In[]:=
JarnikPrimTree[M2,2,AspectRatio1/3,SelectionRuleLast];
{{{2,7},1},{{7,6},1},{{6,1},-4},{{6,3},1},{{3,4},-3},{{4,5},1}} |
Total Weight-3 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Selected Edge | |
2 | 2 | ★ | ∞ | 2 | ∞ | 3 |
| {2,7}1 | |
7 | 2 | ★ | ∞ | 2 | ∞ |
| ★ | {7,6}1 | |
6 |
| ★ | 1 | 2 | 2 | ★ | ★ | {6,1}-4 | |
1 | ★ | ★ |
| 2 | 2 | ★ | ★ | {6,3}1 | |
3 | ★ | ★ | ★ |
| 2 | ★ | ★ | {3,4}-3 | |
4 | ★ | ★ | ★ | ★ |
| ★ | ★ | {4,5}1 |
In[]:=
BoruvkaKruskalTree[M2,AspectRatio1/3,SelectionRuleLast];
{{{1,6},-4},{{3,4},-3},{{6,7},1},{{4,5},1},{{3,6},1},{{2,7},1}} |
Total Weight-3 |
{{{1,6},-4},{{3,4},-3},{{2,7},1},{{3,6},1},{{4,5},1},{{6,7},1},{{1,2},2},{{1,3},2},{{1,5},2},{{1,7},2},{{2,4},2},{{4,6},2},{{5,6},2},{{1,4},3},{{2,6},3},{{3,5},4},{{4,7},4}}
Selected edge | Forest components |
{1,6}-4 | {{1,6}} |
{3,4}-3 | {{1,6},{3,4}} |
{6,7}1 | {{1,6,7},{3,4}} |
{4,5}1 | {{1,6,7},{3,4,5}} |
{3,6}1 | {{1,3,4,5,6,7}} |
{2,7}1 | {{1,2,3,4,5,6,7}} |
Example 5
Example 5
In[]:=
M3=
;
∞ | 1 | -2 | 4 | -1 | 2 | 3 |
1 | ∞ | ∞ | -2 | 4 | ∞ | -4 |
-2 | ∞ | ∞ | 1 | 3 | 2 | -2 |
4 | -2 | 1 | ∞ | -3 | -2 | -1 |
-1 | 4 | 3 | -3 | ∞ | 1 | 4 |
2 | ∞ | 2 | -2 | 1 | ∞ | -3 |
3 | -4 | -2 | -1 | 4 | -3 | ∞ |
In[]:=
EM3=ToWeightedEdgeList[M3];gM3=ToEdgeWeightedGraph[M3];EM3
Out[]=
{{{1,2},1},{{1,3},-2},{{1,4},4},{{1,5},-1},{{1,6},2},{{1,7},3},{{2,4},-2},{{2,5},4},{{2,7},-4},{{3,4},1},{{3,5},3},{{3,6},2},{{3,7},-2},{{4,5},-3},{{4,6},-2},{{4,7},-1},{{5,6},1},{{5,7},4},{{6,7},-3}}
In[]:=
JarnikPrimTree[EM3,3,SelectionRuleRandomChoice,SortTrue,VertexCoordinatesEllipse[6,2]];
{{{1,3},-2},{{2,4},-2},{{2,7},-4},{{3,7},-2},{{4,5},-3},{{6,7},-3}} |
Total Weight-16 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | Selected Edge | |
3 | -2 | ∞ | ★ | 1 | 3 | 2 |
| {3,7}-2 | |
7 | -2 |
| ★ | -1 | 3 | -3 | ★ | {2,7}-4 | |
2 | -2 | ★ | ★ | -2 | 3 |
| ★ | {6,7}-3 | |
6 | -2 | ★ | ★ |
| 1 | ★ | ★ | {2,4}-2 | |
4 | -2 | ★ | ★ | ★ |
| ★ | ★ | {4,5}-3 | |
5 |
| ★ | ★ | ★ | ★ | ★ | ★ | {1,3}-2 |
In[]:=
BoruvkaKruskalTree[EM3,SelectionRuleRandomChoice,SortTrue,TraceTrue,VertexCoordinatesEllipse[6,2]];
{{{1,3},-2},{{2,7},-4},{{3,7},-2},{{4,5},-3},{{4,6},-2},{{6,7},-3}} |
Total Weight-16 |
{{{2,7},-4},{{4,5},-3},{{6,7},-3},{{1,3},-2},{{2,4},-2},{{3,7},-2},{{4,6},-2},{{1,5},-1},{{4,7},-1},{{1,2},1},{{3,4},1},{{5,6},1},{{1,6},2},{{3,6},2},{{1,7},3},{{3,5},3},{{1,4},4},{{2,5},4},{{5,7},4}}
Selected edge | Forest components |
{2,7}-4 | {{2,7}} |
{4,5}-3 | {{2,7},{4,5}} |
{6,7}-3 | {{2,6,7},{4,5}} |
{4,6}-2 | {{2,4,5,6,7}} |
{1,3}-2 | {{2,4,5,6,7},{1,3}} |
{3,7}-2 | {{1,2,3,4,5,6,7}} |
Example 6
Example 6
In[]:=
M4=
;
∞ | -1 | ∞ | -2 | 4 | 3 | 1 | ∞ |
-1 | ∞ | ∞ | ∞ | 4 | -1 | 3 | -1 |
∞ | ∞ | ∞ | -3 | ∞ | 3 | 2 | ∞ |
-2 | ∞ | -3 | ∞ | ∞ | ∞ | 3 | 3 |
4 | 4 | ∞ | ∞ | ∞ | -4 | -1 | 2 |
3 | -1 | 3 | ∞ | -4 | ∞ | 4 | 4 |
1 | 3 | 2 | 3 | -1 | 4 | ∞ | -4 |
∞ | -1 | ∞ | 3 | 2 | 4 | -4 | ∞ |
In[]:=
EM4=ToWeightedEdgeList[M4];gM4=ToEdgeWeightedGraph[M4];EM4
Out[]=
{{{1,2},-1},{{1,4},-2},{{1,5},4},{{1,6},3},{{1,7},1},{{2,5},4},{{2,6},-1},{{2,7},3},{{2,8},-1},{{3,4},-3},{{3,6},3},{{3,7},2},{{4,7},3},{{4,8},3},{{5,6},-4},{{5,7},-1},{{5,8},2},{{6,7},4},{{6,8},4},{{7,8},-4}}
In[]:=
JarnikPrimTree[gM4,4,SelectionRuleRandomChoice,SortTrue,TraceFalse,VertexCoordinatesEllipse[6,2]];
{{{1,2},-1},{{1,4},-2},{{2,6},-1},{{2,8},-1},{{3,4},-3},{{5,6},-4},{{7,8},-4}} |
Total Weight-16 |
In[]:=
BoruvkaKruskalTree[gM4,SelectionRuleRandomChoice,SortTrue,TraceFalse,VertexCoordinatesEllipse[6,2]];
{{{1,2},-1},{{1,4},-2},{{2,6},-1},{{2,8},-1},{{3,4},-3},{{5,6},-4},{{7,8},-4}} |
Total Weight-16 |
Example 7
Example 7
In[]:=
M5=
;
∞ | ∞ | -3 | 3 | ∞ | ∞ | 2 | 4 |
∞ | ∞ | 2 | -3 | 2 | 4 | ∞ | 3 |
-3 | 2 | ∞ | 2 | -2 | -1 | 4 | ∞ |
3 | -3 | 2 | ∞ | ∞ | ∞ | ∞ | 3 |
∞ | 2 | -2 | ∞ | ∞ | ∞ | -1 | ∞ |
∞ | 4 | -1 | ∞ | ∞ | ∞ | 3 | -2 |
2 | ∞ | 4 | ∞ | -1 | 3 | ∞ | ∞ |
4 | 3 | ∞ | 3 | ∞ | -2 | ∞ | ∞ |
In[]:=
EM5=ToWeightedEdgeList[M5];gM5=ToEdgeWeightedGraph[M5];EM5
Out[]=
{{{1,3},-3},{{1,4},3},{{1,7},2},{{1,8},4},{{2,3},2},{{2,4},-3},{{2,5},2},{{2,6},4},{{2,8},3},{{3,4},2},{{3,5},-2},{{3,6},-1},{{3,7},4},{{4,8},3},{{5,7},-1},{{6,7},3},{{6,8},-2}}
In[]:=
JarnikPrimTree[M5,5,ShowTreeFalse,TraceFalse];
{{{5,3},-2},{{3,1},-3},{{3,6},-1},{{6,8},-2},{{5,7},-1},{{5,2},2},{{2,4},-3}} |
Total Weight-10 |
In[]:=
BoruvkaKruskalTree[EM5,ShowTreeFalse,TraceFalse];
{{{1,3},-3},{{2,4},-3},{{3,5},-2},{{6,8},-2},{{3,6},-1},{{5,7},-1},{{2,3},2}} |
Total Weight-10 |
Example 8
Example 8
In[]:=
M6=
;
∞ | ∞ | -2 | 1 | -1 | 1 | ∞ | -1 | 1 | ∞ |
∞ | ∞ | 2 | 4 | ∞ | ∞ | ∞ | ∞ | ∞ | -4 |
-2 | 2 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 2 | ∞ |
1 | 4 | ∞ | ∞ | -3 | -3 | -2 | ∞ | ∞ | 3 |
-1 | ∞ | ∞ | -3 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
1 | ∞ | ∞ | -3 | ∞ | ∞ | ∞ | ∞ | ∞ | -4 |
∞ | ∞ | ∞ | -2 | ∞ | ∞ | ∞ | -3 | -4 | 3 |
-1 | ∞ | ∞ | ∞ | ∞ | ∞ | -3 | ∞ | ∞ | ∞ |
1 | ∞ | 2 | ∞ | 1 | ∞ | -4 | ∞ | ∞ | 3 |
∞ | -4 | ∞ | 3 | ∞ | -4 | 3 | ∞ | 3 | ∞ |
In[]:=
EM6=ToWeightedEdgeList[M6];gM6=ToEdgeWeightedGraph[M6];EM6
Out[]=
{{{1,3},-2},{{1,4},1},{{1,5},-1},{{1,6},1},{{1,8},-1},{{1,9},1},{{2,3},2},{{2,4},4},{{2,10},-4},{{3,9},2},{{4,5},-3},{{4,6},-3},{{4,7},-2},{{4,10},3},{{5,9},1},{{6,10},-4},{{7,8},-3},{{7,9},-4},{{7,10},3},{{9,10},3}}
In[]:=
JarnikPrimTree[EM6,6,ShowTreeFalse,TraceTrue,TreeFalse];
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Selected Edge | |
6 | 1 | ∞ | ∞ | -3 | ∞ | ★ | ∞ | ∞ | ∞ |
| {6,10}-4 | |
10 | 1 |
| ∞ | -3 | ∞ | ★ | 3 | ∞ | 3 | ★ | {10,2}-4 | |
2 | 1 | ★ | 2 |
| ∞ | ★ | 3 | ∞ | 3 | ★ | {6,4}-3 | |
4 | 1 | ★ | 2 | ★ |
| ★ | -2 | ∞ | 3 | ★ | {4,5}-3 | |
5 | -1 | ★ | 2 | ★ | ★ | ★ |
| ∞ | 1 | ★ | {4,7}-2 | |
7 | -1 | ★ | 2 | ★ | ★ | ★ | ★ | -3 |
| ★ | {7,9}-4 | |
9 | -1 | ★ | 2 | ★ | ★ | ★ | ★ |
| ★ | ★ | {7,8}-3 | |
8 |
| ★ | 2 | ★ | ★ | ★ | ★ | ★ | ★ | ★ | {5,1}-1 | |
1 | ★ | ★ |
| ★ | ★ | ★ | ★ | ★ | ★ | ★ | {1,3}-2 |
In[]:=
BoruvkaKruskalTree[EM6,ShowTreeFalse,TraceTrue,TreeFalse];
{{{2,10},-4},{{6,10},-4},{{7,9},-4},{{4,5},-3},{{4,6},-3},{{7,8},-3},{{1,3},-2},{{4,7},-2},{{1,5},-1},{{1,8},-1},{{1,4},1},{{1,6},1},{{1,9},1},{{5,9},1},{{2,3},2},{{3,9},2},{{4,10},3},{{7,10},3},{{9,10},3},{{2,4},4}}
Selected edge | Forest components |
{2,10}-4 | {{2,10}} |
{6,10}-4 | {{2,6,10}} |
{7,9}-4 | {{2,6,10},{7,9}} |
{4,5}-3 | {{2,6,10},{7,9},{4,5}} |
{4,6}-3 | {{2,4,5,6,10},{7,9}} |
{7,8}-3 | {{2,4,5,6,10},{7,8,9}} |
{1,3}-2 | {{2,4,5,6,10},{7,8,9},{1,3}} |
{4,7}-2 | {{2,4,5,6,7,8,9,10},{1,3}} |
{1,5}-1 | {{1,2,3,4,5,6,7,8,9,10}} |
Example 9
Example 9
In[]:=
M7=
;
∞ | 3 | ∞ | -2 | 7 | 2 | ∞ | 9 | ∞ | ∞ |
3 | ∞ | -2 | ∞ | ∞ | 3 | 8 | 5 | 2 | ∞ |
∞ | -2 | ∞ | ∞ | ∞ | 4 | ∞ | -2 | 1 | ∞ |
-2 | ∞ | ∞ | ∞ | 9 | 1 | 2 | ∞ | 7 | 3 |
7 | ∞ | ∞ | 9 | ∞ | ∞ | -2 | ∞ | ∞ | -1 |
2 | 3 | 4 | 1 | ∞ | ∞ | ∞ | 6 | ∞ | 6 |
∞ | 8 | ∞ | 2 | -2 | ∞ | ∞ | ∞ | 3 | ∞ |
9 | 5 | -2 | ∞ | ∞ | 6 | ∞ | ∞ | 1 | ∞ |
∞ | 2 | 1 | 7 | ∞ | ∞ | 3 | 1 | ∞ | 9 |
∞ | ∞ | ∞ | 3 | -1 | 6 | ∞ | ∞ | 9 | ∞ |
In[]:=
EM7=ToWeightedEdgeList[M7];gM7=ToEdgeWeightedGraph[M7];EM7
Out[]=
{{{1,2},3},{{1,4},-2},{{1,5},7},{{1,6},2},{{1,8},9},{{2,3},-2},{{2,6},3},{{2,7},8},{{2,8},5},{{2,9},2},{{3,6},4},{{3,8},-2},{{3,9},1},{{4,5},9},{{4,6},1},{{4,7},2},{{4,9},7},{{4,10},3},{{5,7},-2},{{5,10},-1},{{6,8},6},{{6,10},6},{{7,9},3},{{8,9},1},{{9,10},9}}
In[]:=
JarnikPrimTree[gM7,7,SelectionRuleRandomChoice,ShowTreeTrue,SortTrue,VertexCoordinatesEllipse[6,3]]
{{{1,4},-2},{{2,3},-2},{{3,8},-2},{{3,9},1},{{4,6},1},{{4,7},2},{{5,7},-2},{{5,10},-1},{{7,9},3}} |
Total Weight-2 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | Selected Edge | |
7 | ∞ | 8 | ∞ | 2 |
| ∞ | ★ | ∞ | 3 | ∞ | {5,7}-2 | |
5 | 7 | 8 | ∞ | 2 | ★ | ∞ | ★ | ∞ | 3 |
| {5,10}-1 | |
10 | 7 | 8 | ∞ |
| ★ | 6 | ★ | ∞ | 3 | ★ | {4,7}2 | |
4 |
| 8 | ∞ | ★ | ★ | 1 | ★ | ∞ | 3 | ★ | {1,4}-2 | |
1 | ★ | 3 | ∞ | ★ | ★ |
| ★ | 9 | 3 | ★ | {4,6}1 | |
6 | ★ | 3 | 4 | ★ | ★ | ★ | ★ | 6 |
| ★ | {7,9}3 | |
9 | ★ | 2 |
| ★ | ★ | ★ | ★ | 1 | ★ | ★ | {3,9}1 | |
3 | ★ | -2 | ★ | ★ | ★ | ★ | ★ |
| ★ | ★ | {3,8}-2 | |
8 | ★ |
| ★ | ★ | ★ | ★ | ★ | ★ | ★ | ★ | {2,3}-2 |
In[]:=
BoruvkaKruskalTree[gM7,SelectionRuleRandomChoice,ShowTreeTrue,SortTrue,VertexCoordinatesEllipse[6,3]];
{{{1,2},3},{{1,4},-2},{{2,3},-2},{{3,8},-2},{{4,6},1},{{4,7},2},{{5,7},-2},{{5,10},-1},{{8,9},1}} |
Total Weight-2 |
{{{1,4},-2},{{2,3},-2},{{3,8},-2},{{5,7},-2},{{5,10},-1},{{3,9},1},{{4,6},1},{{8,9},1},{{1,6},2},{{2,9},2},{{4,7},2},{{1,2},3},{{2,6},3},{{4,10},3},{{7,9},3},{{3,6},4},{{2,8},5},{{6,8},6},{{6,10},6},{{1,5},7},{{4,9},7},{{2,7},8},{{1,8},9},{{4,5},9},{{9,10},9}}
Selected edge | Forest components |
{3,8}-2 | {{3,8}} |
{1,4}-2 | {{3,8},{1,4}} |
{2,3}-2 | {{2,3,8},{1,4}} |
{5,7}-2 | {{2,3,8},{1,4},{5,7}} |
{5,10}-1 | {{2,3,8},{1,4},{5,7,10}} |
{4,6}1 | {{2,3,8},{1,4,6},{5,7,10}} |
{8,9}1 | {{2,3,8,9},{1,4,6},{5,7,10}} |
{4,7}2 | {{2,3,8,9},{1,4,5,6,7,10}} |
{1,2}3 | {{1,2,3,4,5,6,7,8,9,10}} |
Strong Components, Condensation and Tarjan’s algorithm
Strong Components, Condensation and Tarjan’s algorithm
Options
Options
Alternatives for optional arguments of Tarjan (default value = the first alternative): -- FontSize -> 10 | Any font size,-- ItemSize -> Automatic | {2, 2, 3 , 3, 3, 14, 14, 5, 12, 2},-- SelectionRule -> First | Last | RandomChoice,-- Sort True | False,- Spacings -> { 0.5, {1, {0.5}}} | Automatic,-- Trace True | Reduced | False,-- TableBreaks False | integer | list of integers > 1.
Alternatives for optional arguments of Condensation: -- AspectRatio -> Automatic | as for Graphics,-- GraphLayout Automatic | as for Graph, -- ImagePadding 10,-- ImageSize -> Automatic | as for Graphics, -- Tarjan -> False | True,--VertexLabels -> “Name” | as for Graph,-- VertexSize -> Automatic | as for Graph,-- VertexStyle -> Automatic | as for Graph.
In[]:=
Options/@{Tarjan,Condensation}//Column[#,Center,Spacings1]&
Out[]=
{FontSize10,ItemSizeAutomatic,SelectionRuleFirst,SortFalse,Spacings{0.5,{1,{0.5}}},TraceTrue,TableBreaksFalse} |
{AspectRatioAutomatic,GraphLayoutAutomatic,ImagePadding10,ImageSizeAutomatic,SortFalse,TarjanTrue,VertexSizeAutomatic,VertexStyleAutomatic} |
Example 10
Example 10
In[]:=
E9={{1,7},{1,10},{2,4},{3,5},{4,6},{5,4},{5,6},{5,7},{5,10},{6,2},{7,10},{8,1},{9,7},{9,8},{10,3}};ME9=ToWeightedAdjacencyMatrix[E9];gE9=ToGraph[E9,DirectedEdgesTrue,GraphLayoutAutomatic,VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE9],StrongComponents[gE9],StrongComponents[ME9],StrongComponents[E9]}//Column
Out[]=
{{2,4,6},{3,5,7,10},{1},{8},{9}} |
{{1},{2,4,6},{3,5,7,10},{8},{9}} |
{{1},{2,4,6},{3,5,7,10},{8},{9}} |
{{1},{2,4,6},{3,5,7,10},{8},{9}} |
In[]:=
Tarjan[gE9,1,SelectionRuleFirst,TraceFalse]
Out[]=
{{4,6,2},{7,10,3,5},{1},{8},{9}}
In[]:=
Tarjan[gE9,1,ItemSizeAutomatic,SelectionRuleFirst,TraceTrue]
{{4,6,2},{7,10,3,5},{1},{8},{9}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 1 | 2 | 1 | 1 | {1,7} | {1,7} | 17 | |
2 | 7 | 1 | 2 | 2 | {1,7,10} | {1,7,10} | 710 | |
3 | 10 | 1 | 3 | 3 | {1,7,10,3} | {1,7,10,3} | 103 | |
4 | 3 | 1 | 4 | 4 | {1,7,10,3,5} | {1,7,10,3,5} | 35 | |
5 | 5 | 4 | 5 | 5 | {1,7,10,3,5,4} | {1,7,10,3,5,4} | 54 | |
6 | 4 | 1 | 6 | 6 | {1,7,10,3,5,4,6} | {1,7,10,3,5,4,6} | 46 | |
7 | 6 | 1 | 7 | 7 | {1,7,10,3,5,4,6,2} | {1,7,10,3,5,4,6,2} | 62 | |
8 | 2 | 1 | 8 | 6 | {1,7,10,3,5,4,6,2} | {1,7,10,3,5,4,6,2} | 24 | |
8 | 6 | 0 | 7 | 6 | {1,7,10,3,5,4,6} | {1,7,10,3,5,4,6,2} | ||
8 | 4 | 0 | 6 | 6 | {1,7,10,3,5,4} | {1,7,10,3,5,4,6,2} | ||
8 | 4 | 0 | 6 | 6 | {1,7,10,3,5} | {1,7,10,3,5} | {4,6,2} | |
8 | 5 | 2 | 5 | 2 | {1,7,10,3,5} | {1,7,10,3,5} | 57 | |
8 | 5 | 1 | 5 | 2 | {1,7,10,3,5} | {1,7,10,3,5} | 510 | |
8 | 3 | 0 | 4 | 2 | {1,7,10,3} | {1,7,10,3,5} | ||
8 | 10 | 0 | 3 | 2 | {1,7,10} | {1,7,10,3,5} | ||
8 | 7 | 0 | 2 | 2 | {1,7} | {1,7,10,3,5} | ||
8 | 7 | 0 | 2 | 2 | {1} | {1} | {7,10,3,5} | |
8 | 1 | 0 | 1 | 1 | {} | {} | {1} | |
8 | 8 | 0 | 8 | 8 | {} | {} | {8} | |
8 | 9 | 0 | 8 | 8 | {} | {} | {9} |
In[]:=
Tarjan[E9,1,ItemSizeAutomatic,SelectionRuleLast,TraceTrue]
{{6,2,4},{10,3,5,7},{1},{8},{9}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 1 | 2 | 1 | 1 | {1,10} | {1,10} | 110 | |
2 | 10 | 1 | 2 | 2 | {1,10,3} | {1,10,3} | 103 | |
3 | 3 | 1 | 3 | 3 | {1,10,3,5} | {1,10,3,5} | 35 | |
4 | 5 | 4 | 4 | 2 | {1,10,3,5} | {1,10,3,5} | 510 | |
4 | 5 | 3 | 4 | 2 | {1,10,3,5,7} | {1,10,3,5,7} | 57 | |
5 | 7 | 1 | 5 | 2 | {1,10,3,5,7} | {1,10,3,5,7} | 710 | |
5 | 5 | 2 | 4 | 2 | {1,10,3,5} | {1,10,3,5,7} | ||
5 | 5 | 2 | 4 | 2 | {1,10,3,5,6} | {1,10,3,5,7,6} | 56 | |
6 | 6 | 1 | 6 | 6 | {1,10,3,5,6,2} | {1,10,3,5,7,6,2} | 62 | |
7 | 2 | 1 | 7 | 7 | {1,10,3,5,6,2,4} | {1,10,3,5,7,6,2,4} | 24 | |
8 | 4 | 1 | 8 | 6 | {1,10,3,5,6,2,4} | {1,10,3,5,7,6,2,4} | 46 | |
8 | 2 | 0 | 7 | 6 | {1,10,3,5,6,2} | {1,10,3,5,7,6,2,4} | ||
8 | 6 | 0 | 6 | 6 | {1,10,3,5,6} | {1,10,3,5,7,6,2,4} | ||
8 | 6 | 0 | 6 | 6 | {1,10,3,5,7} | {1,10,3,5,7} | {6,2,4} | |
8 | 5 | 0 | 4 | 2 | {1,10,3,5} | {1,10,3,5,7} | ||
8 | 3 | 0 | 3 | 2 | {1,10,3} | {1,10,3,5,7} | ||
8 | 10 | 0 | 2 | 2 | {1,10} | {1,10,3,5,7} | ||
8 | 10 | 0 | 2 | 2 | {1} | {1} | {10,3,5,7} | |
8 | 1 | 0 | 1 | 1 | {} | {} | {1} | |
8 | 9 | 1 | 8 | 8 | {9,8} | {9,8} | 98 | |
9 | 8 | 0 | 9 | 9 | {9} | {9} | {8} | |
9 | 9 | 0 | 8 | 8 | {} | {} | {9} |
In[]:=
Tarjan[ME9,1,ItemSizeAutomatic,SelectionRuleRandomChoice,TraceTrue]
{{6,2,4},{7,10,3,5},{1},{8},{9}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 1 | 2 | 1 | 1 | {1,7} | {1,7} | 17 | |
2 | 7 | 1 | 2 | 2 | {1,7,10} | {1,7,10} | 710 | |
3 | 10 | 1 | 3 | 3 | {1,7,10,3} | {1,7,10,3} | 103 | |
4 | 3 | 1 | 4 | 4 | {1,7,10,3,5} | {1,7,10,3,5} | 35 | |
5 | 5 | 4 | 5 | 5 | {1,7,10,3,5,6} | {1,7,10,3,5,6} | 56 | |
6 | 6 | 1 | 6 | 6 | {1,7,10,3,5,6,2} | {1,7,10,3,5,6,2} | 62 | |
7 | 2 | 1 | 7 | 7 | {1,7,10,3,5,6,2,4} | {1,7,10,3,5,6,2,4} | 24 | |
8 | 4 | 1 | 8 | 6 | {1,7,10,3,5,6,2,4} | {1,7,10,3,5,6,2,4} | 46 | |
8 | 2 | 0 | 7 | 6 | {1,7,10,3,5,6,2} | {1,7,10,3,5,6,2,4} | ||
8 | 6 | 0 | 6 | 6 | {1,7,10,3,5,6} | {1,7,10,3,5,6,2,4} | ||
8 | 6 | 0 | 6 | 6 | {1,7,10,3,5} | {1,7,10,3,5} | {6,2,4} | |
8 | 5 | 2 | 5 | 2 | {1,7,10,3,5} | {1,7,10,3,5} | 57 | |
8 | 5 | 1 | 5 | 2 | {1,7,10,3,5} | {1,7,10,3,5} | 510 | |
8 | 3 | 0 | 4 | 2 | {1,7,10,3} | {1,7,10,3,5} | ||
8 | 10 | 0 | 3 | 2 | {1,7,10} | {1,7,10,3,5} | ||
8 | 7 | 0 | 2 | 2 | {1,7} | {1,7,10,3,5} | ||
8 | 7 | 0 | 2 | 2 | {1} | {1} | {7,10,3,5} | |
8 | 1 | 0 | 1 | 1 | {} | {} | {1} | |
8 | 9 | 1 | 8 | 8 | {9,8} | {9,8} | 98 | |
9 | 8 | 0 | 9 | 9 | {9} | {9} | {8} | |
9 | 9 | 0 | 8 | 8 | {} | {} | {9} |
In[]:=
Condensation[E9,GraphLayout"SpringEmbedding",ImageSize{300,Automatic},VertexSize0.5Small,VertexStyleRed]
Out[]=
{1{4,6,2},2{7,10,3,5},3{1},4{8},5{9}} |
Example 11
Example 11
In[]:=
E10={12,21,31,48,49,59,52,510,51,610,69,64,74,82,87,98,105,106,104};gE10=ToGraph[E10,GraphLayoutAutomatic,VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE10],StrongComponents[E10]}//Column
Out[]=
{{1,2},{3},{4,7,8,9},{5,6,10}} |
{{1,2},{3},{4,7,8,9},{5,6,10}} |
In[]:=
SeedRandom[20!];Tarjan[E10,2,SelectionRuleRandomChoice,SortTrue,TraceReduce]
{{3},{1,2},{5,6,10},{4,7,8,9}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 2 | 1 | 1 | 1 | {2,1} | {2,1} | 21 | |
2 | 1 | 1 | 2 | 1 | {2,1} | {2,1} | 12 | |
2 | 2 | 0 | 1 | 1 | {} | {} | {2,1} | |
2 | 8 | 1 | 2 | 2 | {8,7} | {8,7} | 87 | |
3 | 7 | 1 | 3 | 3 | {8,7,4} | {8,7,4} | 74 | |
4 | 4 | 2 | 4 | 4 | {8,7,4,9} | {8,7,4,9} | 49 | |
5 | 9 | 1 | 5 | 2 | {8,7,4,9} | {8,7,4,9} | 98 | |
5 | 4 | 1 | 4 | 2 | {8,7,4} | {8,7,4,9} | 48 | |
5 | 8 | 0 | 2 | 2 | {} | {} | {8,7,4,9} | |
5 | 5 | 1 | 5 | 5 | {5,10} | {5,10} | 510 | |
6 | 10 | 2 | 6 | 5 | {5,10} | {5,10} | 105 | |
6 | 10 | 1 | 6 | 5 | {5,10,6} | {5,10,6} | 106 | |
7 | 6 | 1 | 7 | 6 | {5,10,6} | {5,10,6} | 610 | |
7 | 5 | 0 | 5 | 5 | {} | {} | {5,10,6} | |
7 | 3 | 0 | 7 | 7 | {} | {} | {3} |
In[]:=
SeedRandom[20!];Tarjan[E10,2,SelectionRuleRandomChoice];
{{2,1},{8,7,4,9},{5,10,6},{3}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 2 | 1 | 1 | 1 | {2,1} | {2,1} | 21 | |
2 | 1 | 1 | 2 | 1 | {2,1} | {2,1} | 12 | |
2 | 2 | 0 | 1 | 1 | {2} | {2,1} | ||
2 | 2 | 0 | 1 | 1 | {} | {} | {2,1} | |
2 | 8 | 1 | 2 | 2 | {8,7} | {8,7} | 87 | |
3 | 7 | 1 | 3 | 3 | {8,7,4} | {8,7,4} | 74 | |
4 | 4 | 2 | 4 | 4 | {8,7,4,9} | {8,7,4,9} | 49 | |
5 | 9 | 1 | 5 | 2 | {8,7,4,9} | {8,7,4,9} | 98 | |
5 | 4 | 1 | 4 | 2 | {8,7,4} | {8,7,4,9} | ||
5 | 4 | 1 | 4 | 2 | {8,7,4} | {8,7,4,9} | 48 | |
5 | 7 | 0 | 3 | 2 | {8,7} | {8,7,4,9} | ||
5 | 8 | 0 | 2 | 2 | {8} | {8,7,4,9} | ||
5 | 8 | 0 | 2 | 2 | {} | {} | {8,7,4,9} | |
5 | 5 | 1 | 5 | 5 | {5,10} | {5,10} | 510 | |
6 | 10 | 2 | 6 | 5 | {5,10} | {5,10} | 105 | |
6 | 10 | 1 | 6 | 5 | {5,10,6} | {5,10,6} | 106 | |
7 | 6 | 1 | 7 | 6 | {5,10,6} | {5,10,6} | 610 | |
7 | 10 | 0 | 6 | 5 | {5,10} | {5,10,6} | ||
7 | 5 | 0 | 5 | 5 | {5} | {5,10,6} | ||
7 | 5 | 0 | 5 | 5 | {} | {} | {5,10,6} | |
7 | 3 | 0 | 7 | 7 | {} | {} | {3} |
In[]:=
Condensation[E10,AspectRatio1/3,ImageSize{300,Automatic},SortTrue,VertexSizeSmall,VertexStyleRed]
Out[]=
{1{3},2{1,2},3{5,6,10},4{4,7,8,9}} |
Example 12
Example 12
In[]:=
E11={110,16,26,36,410,43,52,59,58,56,68,78,71,82,95,106,107,104};gE11=ToGraph[E11];Graph[E11,VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE11],StrongComponents[gE11]}//Column
Out[]=
{{2,6,8},{3},{1,4,7,10},{5,9}} |
{{1,4,7,10},{2,6,8},{3},{5,9}} |
In[]:=
SeedRandom[20!];Tarjan[gE11,6,SelectionRuleRandomChoice]
{{6,8,2},{9,5},{3},{1,10,7,4}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 6 | 1 | 1 | 1 | {6,8} | {6,8} | 68 | |
2 | 8 | 1 | 2 | 2 | {6,8,2} | {6,8,2} | 82 | |
3 | 2 | 1 | 3 | 1 | {6,8,2} | {6,8,2} | 26 | |
3 | 8 | 0 | 2 | 1 | {6,8} | {6,8,2} | ||
3 | 6 | 0 | 1 | 1 | {6} | {6,8,2} | ||
3 | 6 | 0 | 1 | 1 | {} | {} | {6,8,2} | |
3 | 9 | 1 | 3 | 3 | {9,5} | {9,5} | 95 | |
4 | 5 | 1 | 4 | 3 | {9,5} | {9,5} | 59 | |
4 | 9 | 0 | 3 | 3 | {9} | {9,5} | ||
4 | 9 | 0 | 3 | 3 | {} | {} | {9,5} | |
4 | 1 | 1 | 4 | 4 | {1,10} | {1,10} | 110 | |
5 | 10 | 2 | 5 | 5 | {1,10,7} | {1,10,7} | 107 | |
6 | 7 | 1 | 6 | 4 | {1,10,7} | {1,10,7} | 71 | |
6 | 10 | 1 | 5 | 4 | {1,10} | {1,10,7} | ||
6 | 10 | 1 | 5 | 4 | {1,10,4} | {1,10,7,4} | 104 | |
7 | 4 | 2 | 7 | 7 | {1,10,4,3} | {1,10,7,4,3} | 43 | |
8 | 3 | 0 | 8 | 8 | {1,10,7,4} | {1,10,7,4} | {3} | |
8 | 4 | 1 | 7 | 5 | {1,10,7,4} | {1,10,7,4} | 410 | |
8 | 7 | 0 | 6 | 4 | {1,10,7} | {1,10,7,4} | ||
8 | 10 | 0 | 5 | 4 | {1,10} | {1,10,7,4} | ||
8 | 1 | 0 | 4 | 4 | {1} | {1,10,7,4} | ||
8 | 1 | 0 | 4 | 4 | {} | {} | {1,10,7,4} |
In[]:=
SeedRandom[20!];Tarjan[gE11,6,SelectionRuleRandomChoice,TraceReduce]
{{6,8,2},{9,5},{3},{1,10,7,4}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 6 | 1 | 1 | 1 | {6,8} | {6,8} | 68 | |
2 | 8 | 1 | 2 | 2 | {6,8,2} | {6,8,2} | 82 | |
3 | 2 | 1 | 3 | 1 | {6,8,2} | {6,8,2} | 26 | |
3 | 6 | 0 | 1 | 1 | {} | {} | {6,8,2} | |
3 | 9 | 1 | 3 | 3 | {9,5} | {9,5} | 95 | |
4 | 5 | 1 | 4 | 3 | {9,5} | {9,5} | 59 | |
4 | 9 | 0 | 3 | 3 | {} | {} | {9,5} | |
4 | 1 | 1 | 4 | 4 | {1,10} | {1,10} | 110 | |
5 | 10 | 2 | 5 | 5 | {1,10,7} | {1,10,7} | 107 | |
6 | 7 | 1 | 6 | 4 | {1,10,7} | {1,10,7} | 71 | |
6 | 10 | 1 | 5 | 4 | {1,10,4} | {1,10,7,4} | 104 | |
7 | 4 | 2 | 7 | 7 | {1,10,4,3} | {1,10,7,4,3} | 43 | |
8 | 3 | 0 | 8 | 8 | {1,10,7,4} | {1,10,7,4} | {3} | |
8 | 4 | 1 | 7 | 5 | {1,10,7,4} | {1,10,7,4} | 410 | |
8 | 1 | 0 | 4 | 4 | {} | {} | {1,10,7,4} |
In[]:=
Condensation[gE11,AspectRatio1/3,ImageSize{300,Automatic},VertexSizeSmall,VertexStyleRed,ImagePadding10]
Out[]=
{1{6,8,2},2{3},3{1,10,7,4},4{5,9}} |
Example 13
Example 13
In[]:=
E12={{1,5},{2,4},{3,8},{4,10},{4,7},{5,4},{5,3},{5,1},{6,2},{8,9},{8,2},{8,10},{8,3},{9,3},{9,7},{9,4},{10,6}};gE12=ToGraph[E12];Graph[E12,DirectedEdgesTrue,VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE12],StrongComponents[gE12]}//Column
Out[]=
{{7},{2,4,6,10},{3,8,9},{1,5}} |
{{1,5},{2,4,6,10},{3,8,9},{7}} |
In[]:=
Tarjan[gE12,2,SortTrue]
{{7},{1,5},{3,8,9},{2,4,6,10}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 2 | 1 | 1 | 1 | {2,4} | {2,4} | 24 | |
2 | 4 | 2 | 2 | 2 | {2,4,7} | {2,4,7} | 47 | |
3 | 7 | 0 | 3 | 3 | {2,4} | {2,4} | {7} | |
3 | 4 | 1 | 2 | 2 | {2,4,10} | {2,4,10} | 410 | |
4 | 10 | 1 | 4 | 4 | {2,4,10,6} | {2,4,10,6} | 106 | |
5 | 6 | 1 | 5 | 1 | {2,4,10,6} | {2,4,10,6} | 62 | |
5 | 10 | 0 | 4 | 1 | {2,4,10} | {2,4,10,6} | ||
5 | 4 | 0 | 2 | 1 | {2,4} | {2,4,10,6} | ||
5 | 2 | 0 | 1 | 1 | {2} | {2,4,10,6} | ||
5 | 2 | 0 | 1 | 1 | {} | {} | {2,4,10,6} | |
5 | 1 | 1 | 5 | 5 | {1,5} | {1,5} | 15 | |
6 | 5 | 2 | 6 | 5 | {1,5} | {1,5} | 51 | |
6 | 5 | 1 | 6 | 5 | {1,5,3} | {1,5,3} | 53 | |
7 | 3 | 1 | 7 | 7 | {1,5,3,8} | {1,5,3,8} | 38 | |
8 | 8 | 2 | 8 | 7 | {1,5,3,8} | {1,5,3,8} | 83 | |
8 | 8 | 1 | 8 | 7 | {1,5,3,8,9} | {1,5,3,8,9} | 89 | |
9 | 9 | 1 | 9 | 7 | {1,5,3,8,9} | {1,5,3,8,9} | 93 | |
9 | 8 | 0 | 8 | 7 | {1,5,3,8} | {1,5,3,8,9} | ||
9 | 3 | 0 | 7 | 7 | {1,5,3} | {1,5,3,8,9} | ||
9 | 3 | 0 | 7 | 7 | {1,5} | {1,5} | {3,8,9} | |
9 | 1 | 0 | 5 | 5 | {1} | {1,5} | ||
9 | 1 | 0 | 5 | 5 | {} | {} | {1,5} |
In[]:=
Condensation[gE12,AspectRatio1/3,ImageSize{300,Automatic},VertexSizeSmall,VertexStyleRed]
Out[]=
{1{7},2{4,10,6,2},3{3,8,9},4{1,5}} |
Example 14
Example 14
In[]:=
E13={{1,2},{1,4},{2,5},{2,10},{3,1},{3,6},{3,9},{3,10},{3,11},{4,2},{4,5},{4,6},{4,7},{4,9},{5,1},{5,6},{5,10},{6,9},{7,6},{8,10},{8,12},{9,7},{9,10},{11,4},{11,7},{11,8},{11,9},{12,7},{12,9},{12,10},{12,11}};gE13=ToGraph[E13];Graph[E13,DirectedEdgesTrue,VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE13],StrongComponents[E13]}//Column
Out[]=
{{10},{6,7,9},{1,2,4,5},{8,11,12},{3}} |
{{1,2,4,5},{3},{6,7,9},{8,11,12},{10}} |
In[]:=
SeedRandom[20!];Tarjan[gE13,10,SelectionRuleRandomChoice,Trace->Reduce]
{{10},{9,7,6},{5,1,2,4},{12,11,8},{3}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 10 | 0 | 1 | 1 | {} | {} | {10} | |
2 | 5 | 2 | 2 | 2 | {5,1} | {5,1} | 51 | |
3 | 1 | 2 | 3 | 3 | {5,1,2} | {5,1,2} | 12 | |
4 | 2 | 1 | 4 | 2 | {5,1,2} | {5,1,2} | 25 | |
4 | 1 | 1 | 3 | 2 | {5,1,4} | {5,1,2,4} | 14 | |
5 | 4 | 5 | 5 | 5 | {5,1,4,9} | {5,1,2,4,9} | 49 | |
6 | 9 | 1 | 6 | 6 | {5,1,4,9,7} | {5,1,2,4,9,7} | 97 | |
7 | 7 | 1 | 7 | 7 | {5,1,4,9,7,6} | {5,1,2,4,9,7,6} | 76 | |
8 | 6 | 1 | 8 | 6 | {5,1,4,9,7,6} | {5,1,2,4,9,7,6} | 69 | |
8 | 9 | 0 | 6 | 6 | {5,1,2,4} | {5,1,2,4} | {9,7,6} | |
8 | 4 | 2 | 5 | 4 | {5,1,2,4} | {5,1,2,4} | 42 | |
8 | 4 | 1 | 5 | 2 | {5,1,2,4} | {5,1,2,4} | 45 | |
8 | 5 | 0 | 2 | 2 | {} | {} | {5,1,2,4} | |
8 | 12 | 1 | 8 | 8 | {12,11} | {12,11} | 1211 | |
9 | 11 | 1 | 9 | 9 | {12,11,8} | {12,11,8} | 118 | |
10 | 8 | 1 | 10 | 8 | {12,11,8} | {12,11,8} | 812 | |
10 | 12 | 0 | 8 | 8 | {} | {} | {12,11,8} | |
10 | 3 | 0 | 10 | 10 | {} | {} | {3} |
In[]:=
SeedRandom[20!];Tarjan[gE13,10,SelectionRuleRandomChoice]
{{10},{9,7,6},{5,1,2,4},{12,11,8},{3}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 10 | 0 | 1 | 1 | {} | {} | {10} | |
2 | 5 | 2 | 2 | 2 | {5,1} | {5,1} | 51 | |
3 | 1 | 2 | 3 | 3 | {5,1,2} | {5,1,2} | 12 | |
4 | 2 | 1 | 4 | 2 | {5,1,2} | {5,1,2} | 25 | |
4 | 1 | 1 | 3 | 2 | {5,1} | {5,1,2} | ||
4 | 1 | 1 | 3 | 2 | {5,1,4} | {5,1,2,4} | 14 | |
5 | 4 | 5 | 5 | 5 | {5,1,4,9} | {5,1,2,4,9} | 49 | |
6 | 9 | 1 | 6 | 6 | {5,1,4,9,7} | {5,1,2,4,9,7} | 97 | |
7 | 7 | 1 | 7 | 7 | {5,1,4,9,7,6} | {5,1,2,4,9,7,6} | 76 | |
8 | 6 | 1 | 8 | 6 | {5,1,4,9,7,6} | {5,1,2,4,9,7,6} | 69 | |
8 | 7 | 0 | 7 | 6 | {5,1,4,9,7} | {5,1,2,4,9,7,6} | ||
8 | 9 | 0 | 6 | 6 | {5,1,4,9} | {5,1,2,4,9,7,6} | ||
8 | 9 | 0 | 6 | 6 | {5,1,2,4} | {5,1,2,4} | {9,7,6} | |
8 | 4 | 2 | 5 | 4 | {5,1,2,4} | {5,1,2,4} | 42 | |
8 | 4 | 1 | 5 | 2 | {5,1,2,4} | {5,1,2,4} | 45 | |
8 | 2 | 0 | 4 | 2 | {5,1,2} | {5,1,2,4} | ||
8 | 1 | 0 | 3 | 2 | {5,1} | {5,1,2,4} | ||
8 | 5 | 0 | 2 | 2 | {5} | {5,1,2,4} | ||
8 | 5 | 0 | 2 | 2 | {} | {} | {5,1,2,4} | |
8 | 12 | 1 | 8 | 8 | {12,11} | {12,11} | 1211 | |
9 | 11 | 1 | 9 | 9 | {12,11,8} | {12,11,8} | 118 | |
10 | 8 | 1 | 10 | 8 | {12,11,8} | {12,11,8} | 812 | |
10 | 11 | 0 | 9 | 8 | {12,11} | {12,11,8} | ||
10 | 12 | 0 | 8 | 8 | {12} | {12,11,8} | ||
10 | 12 | 0 | 8 | 8 | {} | {} | {12,11,8} | |
10 | 3 | 0 | 10 | 10 | {} | {} | {3} |
In[]:=
Condensation[gE13,DirectedEdgesTrue,VertexStyleRed,ImagePadding10]
Out[]=
{1{10},2{6,9,7},3{1,2,5,4},4{11,8,12},5{3}} |
Example 15
Example 15
In[]:=
E14={42,39,18,51,13,67,46,62,48,45,410,910,29,59,710,93,210,74,103,53,79,69,23,15,110};gE14=Graph[E14,ImageSize{300,Automatic},VertexLabels->"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
{ConnectedComponents[gE14],StrongComponents[gE14]}//Column
Out[]=
{{3,9,10},{2},{8},{1,5},{4,6,7}} |
{{4,6,7},{1,5},{2},{3,9,10},{8}} |
In[]:=
Tarjan[gE14,4,Spacings{1,{1,0.5}},TableBreaks{14}]
{{9,10,3},{2},{8},{5,1},{4,6,7}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 4 | 5 | 1 | 1 | {4,2} | {4,2} | 42 | |
2 | 2 | 3 | 2 | 2 | {4,2,9} | {4,2,9} | 29 | |
3 | 9 | 2 | 3 | 3 | {4,2,9,10} | {4,2,9,10} | 910 | |
4 | 10 | 1 | 4 | 4 | {4,2,9,10,3} | {4,2,9,10,3} | 103 | |
5 | 3 | 1 | 5 | 3 | {4,2,9,10,3} | {4,2,9,10,3} | 39 | |
5 | 10 | 0 | 4 | 3 | {4,2,9,10} | {4,2,9,10,3} | ||
5 | 9 | 1 | 3 | 3 | {4,2,9} | {4,2,9,10,3} | ||
5 | 9 | 1 | 3 | 3 | {4,2,9} | {4,2,9,10,3} | 93 | |
5 | 9 | 0 | 3 | 3 | {4,2} | {4,2} | {9,10,3} | |
5 | 2 | 0 | 2 | 2 | {4} | {4} | {2} | |
5 | 4 | 3 | 1 | 1 | {4,6} | {4,6} | 46 | |
6 | 6 | 1 | 6 | 6 | {4,6,7} | {4,6,7} | 67 | |
7 | 7 | 1 | 7 | 1 | {4,6,7} | {4,6,7} | 74 | |
7 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} |
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
7 | 4 | 2 | 1 | 1 | {4} | {4,6,7} | ||
7 | 4 | 2 | 1 | 1 | {4,8} | {4,6,7,8} | 48 | |
8 | 8 | 0 | 8 | 8 | {4,6,7} | {4,6,7} | {8} | |
8 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} | ||
8 | 4 | 1 | 1 | 1 | {4} | {4,6,7} | ||
8 | 4 | 1 | 1 | 1 | {4,5} | {4,6,7,5} | 45 | |
9 | 5 | 1 | 9 | 9 | {4,5,1} | {4,6,7,5,1} | 51 | |
10 | 1 | 1 | 10 | 9 | {4,5,1} | {4,6,7,5,1} | 15 | |
10 | 5 | 0 | 9 | 9 | {4,5} | {4,6,7,5,1} | ||
10 | 5 | 0 | 9 | 9 | {4,6,7} | {4,6,7} | {5,1} | |
10 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} | ||
10 | 4 | 0 | 1 | 1 | {4} | {4,6,7} | ||
10 | 4 | 0 | 1 | 1 | {} | {} | {4,6,7} |
In[]:=
Tarjan[E14,4,SortFalse,Spacings{1,{1,0.5}},TableBreaks{14}]
{{9,10,3},{2},{8},{5,1},{4,6,7}}
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
1 | 4 | 5 | 1 | 1 | {4,2} | {4,2} | 42 | |
2 | 2 | 3 | 2 | 2 | {4,2,9} | {4,2,9} | 29 | |
3 | 9 | 2 | 3 | 3 | {4,2,9,10} | {4,2,9,10} | 910 | |
4 | 10 | 1 | 4 | 4 | {4,2,9,10,3} | {4,2,9,10,3} | 103 | |
5 | 3 | 1 | 5 | 3 | {4,2,9,10,3} | {4,2,9,10,3} | 39 | |
5 | 10 | 0 | 4 | 3 | {4,2,9,10} | {4,2,9,10,3} | ||
5 | 9 | 1 | 3 | 3 | {4,2,9} | {4,2,9,10,3} | ||
5 | 9 | 1 | 3 | 3 | {4,2,9} | {4,2,9,10,3} | 93 | |
5 | 9 | 0 | 3 | 3 | {4,2} | {4,2} | {9,10,3} | |
5 | 2 | 0 | 2 | 2 | {4} | {4} | {2} | |
5 | 4 | 3 | 1 | 1 | {4,6} | {4,6} | 46 | |
6 | 6 | 1 | 6 | 6 | {4,6,7} | {4,6,7} | 67 | |
7 | 7 | 1 | 7 | 1 | {4,6,7} | {4,6,7} | 74 | |
7 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} |
i | x | + d | P(x) | Z(x) | ST1 | ST2 | e | c |
7 | 4 | 2 | 1 | 1 | {4} | {4,6,7} | ||
7 | 4 | 2 | 1 | 1 | {4,8} | {4,6,7,8} | 48 | |
8 | 8 | 0 | 8 | 8 | {4,6,7} | {4,6,7} | {8} | |
8 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} | ||
8 | 4 | 1 | 1 | 1 | {4} | {4,6,7} | ||
8 | 4 | 1 | 1 | 1 | {4,5} | {4,6,7,5} | 45 | |
9 | 5 | 1 | 9 | 9 | {4,5,1} | {4,6,7,5,1} | 51 | |
10 | 1 | 1 | 10 | 9 | {4,5,1} | {4,6,7,5,1} | 15 | |
10 | 5 | 0 | 9 | 9 | {4,5} | {4,6,7,5,1} | ||
10 | 5 | 0 | 9 | 9 | {4,6,7} | {4,6,7} | {5,1} | |
10 | 6 | 0 | 6 | 1 | {4,6} | {4,6,7} | ||
10 | 4 | 0 | 1 | 1 | {4} | {4,6,7} | ||
10 | 4 | 0 | 1 | 1 | {} | {} | {4,6,7} |
In[]:=
Condensation[E14,AspectRatio1/3,DirectedEdgesTrue,ImageSize{300,Automatic},SortTrue,VertexSizeSmall,VertexStyleRed,ImagePadding10]
Out[]=
{1{2},2{8},3{1,5},4{3,9,10},5{4,6,7}} |
Topological Order, TopologicalOrderTest
Topological Order, TopologicalOrderTest
Options
Options
Alternatives for optional arguments of TopologicalOrder:
-- EdgeList → True | False,
-- FontSize -> 10 | Any font size,
-- Print -> False | True,
-- SelectionRule ->First | Last | RandomChoice,
-- Sort False | True,
-- Spacings -> {1, 1},
-- TableBreaks -> False | integer | list of integers,
-- Trace -> True | False,
-- Vertices True | False.
-- EdgeList → True | False,
-- FontSize -> 10 | Any font size,
-- Print -> False | True,
-- SelectionRule ->First | Last | RandomChoice,
-- Sort False | True,
-- Spacings -> {1, 1},
-- TableBreaks -> False | integer | list of integers,
-- Trace -> True | False,
-- Vertices True | False.
Alternatives for optional arguments of TopologicalOrderTest:
-- Trace -> True | False.
-- Trace -> True | False.
In[]:=
Options[TopologicalOrder]
Out[]=
{EdgeListTrue,FontSize10,Method1,PrintFalse,SelectionRuleFirst,SortFalse,Spacings{0.5,1},TableBreaksFalse,TraceFalse,VertexListTrue}
Example 16
Example 16
In[]:=
V15=Range[12];E15={14,15,110,111,112,24,210,312,412,58,512,62,63,68,71,72,78,79,710,712,89,114,118,1110,1210};ME15=ToWeightedAdjacencyMatrix[E15];gE15=ToGraph[E15];ToGraph[E15,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,12],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE15,Method1,EdgeListTrue,Spacings{0.5,1},TraceTrue]
Out[]=
{{6,7},{1,2,3},{5,11},{4,8},{9,12},{10}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{6{2,3,8},7{1,2,8,9,10,12},1{4,5,10,11,12},2{4,10},3{12},5{8,12},11{4,8,10},4{12},8{9},12{10}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In[]:=
TopologicalOrderTest[gE15,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
In[]:=
TopologicalOrder[gE15,Method2,EdgeListTrue,Spacings{0.5,1},TableBreaks8,TraceTrue]
Out[]=
{6,7,3,1,2,5,11,4,8,12,9,10} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{6{2,3,8},7{1,2,8,9,10,12},3{12},1{4,5,10,11,12},2{4,10},5{8,12},11{4,8,10},4{12},8{9},12{10}} | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In[]:=
TopologicalOrderTest[gE15,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
In[]:=
TopologicalOrder[gE15,Method3,EdgeListTrue,TableBreaks{9,16},TraceTrue]
Out[]=
{6,7,3,1,2,5,11,4,8,12,9,10} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{6{2,3,8},7{1,2,8,9,10,12},3{12},1{4,5,10,11,12},2{4,10},5{8,12},11{4,8,10},4{12},8{9},12{10}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In[]:=
TopologicalOrderTest[gE15,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
Example 17
Example 17
In[]:=
V16=Range[12];E16={12,29,43,411,52,54,56,58,510,511,512,61,63,611,74,711,81,83,86,87,812,101,102,106,109,1011,1012,112,119,122};gE16=Graph[V16,E16,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,12],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE16,Method1,TraceTrue]
Out[]=
{{5},{8,10},{6,7,12},{1,4},{3,11},{2},{9}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{5{2,4,6,8,10,11,12},8{1,3,6,7,12},10{1,2,6,9,11,12},6{1,3,11},7{4,11},12{2},1{2},4{3,11},11{2,9},2{9}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In[]:=
TopologicalOrderTest[E16,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
Example 18
Example 18
In[]:=
V17=Range[12];E17={12,17,110,27,31,412,52,58,511,62,64,65,68,87,812,93,94,96,97,98,910,912,107,111,112,114,117,118,1112,127};gE17=Graph[V17,E17,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,12],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE17,Method2,Spacings{0.3,1},TraceTrue]
Out[]=
{9,3,6,5,11,1,4,2,8,10,12,7} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{9{3,4,6,7,8,10,12},3{1},6{2,4,5,8},5{2,8,11},11{1,2,4,7,8,12},1{2,7,10},4{12},2{7},8{7,12},10{7},12{7}} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
In[]:=
TopologicalOrderTest[gE17,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
Example 19
Example 19
In[]:=
V18=Range[12];E18={15,110,23,24,26,210,212,53,65,610,72,75,76,81,83,87,94,103,104,111,112,115,116,117,118,119,1110,123,124,125};gE18=Graph[V18,E18,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,12],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE18]
Out[]=
{{11},{8,9},{1,7},{2},{6,12},{5,10},{3,4}} |
{11{1,2,5,6,7,8,9,10},8{1,3,7},9{4},1{5,10},7{2,5,6},2{3,4,6,10,12},6{5,10},12{3,4,5},5{3},10{3,4}} |
In[]:=
TopologicalOrderTest[gE18,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
Example 20
Example 20
In[]:=
V19=Range[13];E19={13,111,29,43,412,413,52,53,59,510,511,62,64,610,76,78,82,813,102,103,118,119,1110,129,139};gE19=Graph[V19,E19,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,13],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE19]
Out[]=
{{1,5,7},{6,11},{4,8,10},{2,3,12,13},{9}} |
{1{3,11},5{2,3,9,10,11},7{6,8},6{2,4,10},11{8,9,10},4{3,12,13},8{2,13},10{2,3},2{9},12{9},13{9}} |
In[]:=
TopologicalOrderTest[gE19,#]&/@{%[[1,1]],%[[1,2]]}
Out[]=
{True,True}
Example 21
Example 21
In[]:=
V20=Range[12];E20={{{1,2},2},{{1,6},12},{{5,2},17},{{2,3},1},{{2,8},1},{{3,1},4},{{4,3},3},{{4,6},4},{{4,8},12},{{5,1},8},{{5,4},10},{{6,2},12},{{6,3},12},{{6,7},10},{{8,7},7},{{9,6},4},{{9,10},1},{{10,11},0},{{11,12},-1},{{12,1},3}};gE20=Graph[V20,First/@E20,AspectRatio0.5,DirectedEdgesTrue,GraphLayoutAutomatic,VertexCoordinatesEllipse[2,1,12],VertexLabels"Name",VertexStyleRed,ImagePadding10]
Out[]=
In[]:=
TopologicalOrder[gE20];
| ||
|
Shortest Paths: DijkstraPathsTree
Shortest Paths: DijkstraPathsTree
Options
Options
Alternatives for optional arguments :-- “ArrowSize” -> Automatic | 0.04 | as for Graph,-- DistanceTable → True | False,-- FontSize -> 10 | as for Grid,-- GraphLayout -> Automatic | any Graph layout, -- ImageSize -> Automatic | {450, 300} | as for Graph,-- ItemSize → Automatic | {6,4,2,3,3,5,8,2} | as for Grid,-- SelectionRule → First | Last | RandomChoice, -- ShowTree -> Tree | False,-- Sort →True | False,-- Spacings -> {1,1} | Automatic | as for Grid, -- TableBreaks → False | integer | list of positive integers,-- Trace -> All | Reduce | False,-- Tree -> True | False,-- TreeEdgeStyle → {Thickness[0.005], Red} | as for EdgeStyle,-- VertexCoordinates Automatic | Ellipse[a, b],-- VertexLabelStyle Directive[Bold, Blue, 12] | as for Graph,-- VertexSize -> Automatic | Tiny | Small | Medium | Large | number | {“Scaled”, number},-- VertexStyle Blue | any color specification.
In[]:=
Options[DijkstraPathsTree]
Out[]=
ArrowSizeAutomatic,DijkstraFalse,DistanceTableTrue,FontSize10,GraphLayoutAutomatic,ImagePadding10,ImageSizeAutomatic,ItemSizeAutomatic,SelectionRuleFirst,ShowTreeTrue,Spacings{0.7,0.7},SortTrue,TableBreaksFalse,TraceTrue,TreeTrue,TreeEdgeStyleThickness[0.003],
,VertexCoordinatesAutomatic,VertexLabelsName,VertexSizeAutomatic,VertexStyle
,VertexLabelStyleDirectiveBold,
,12
Example 22
Example 22
In[]:=
E13={{{1,2},2},{{1,6},12},{{5,2},17},{{2,3},1},{{2,8},1},{{3,1},-4},{{9,6},4},{{4,3},3},{{4,6},4},{{4,8},12},{{5,1},8},{{5,4},10},{{6,2},12},{{6,3},12},{{6,7},10},{{8,7},7}};ME13=ToWeightedAdjacencyMatrix[E13];gE13=ToEdgeWeightedGraph[E13];ME13//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 17 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@E13],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 8 | 9 |
y | 2 | 6 | 3 | 8 | 1 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 3 | 7 | 7 | 6 |
w | 2 | 12 | 1 | 1 | -4 | 3 | 4 | 12 | 8 | 17 | 10 | 12 | 12 | 10 | 7 | 4 |
In[]:=
DijkstraPathsTree[gE13,6,VertexCoordinatesEllipse[6,3],VertexSizeSmall]
Found cycle of negative totalWeight |
{{31,-4},{12,2},{23,1}} |
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{6} | 62 | 12 | 0 | ∞ | 12 | 62 | 2 |
{6,2} | 63 | 12 | 0 | ∞ | 12 | 63 | 3 |
{6,2,3} | 67 | 10 | 0 | ∞ | 10 | 67 | 7 |
{2,3,7} | 23 | 1 | 12 | 12 | 13 | ||
{2,3,7} | 28 | 1 | 12 | ∞ | 13 | 28 | 8 |
{3,7,8} | 31 | -4 | 12 | ∞ | 8 | 31 | 1 |
{8,1} | 87 | 7 | 13 | 10 | 20 | ||
{1} | 12 | 2 | 8 | 12 | 10 | 12 | 2 |
{1,2} | 16 | 12 | 8 | 0 | 20 | ||
{2} | 23 | 1 | 10 | 12 | 11 | 23 | 3 |
Example 23
Example 23
In[]:=
E14={{{1,2},2},{{1,6},12},{{5,2},10},{{2,3},1},{{8,2},1},{{4,3},-3},{{4,6},4},{{4,8},2},{{5,1},10},{{5,4},10},{{6,9},-5},{{6,2},12},{{3,9},5},{{6,7},10},{{7,9},1},{{8,7},7},{{9,8},-7}};ME14=ToWeightedAdjacencyMatrix[E14];gE14=ToEdgeWeightedGraph[E14];ME14//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | ∞ | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
In[]:=
gE14//EdgeList
Out[]=
{12,16,52,23,82,43,46,48,51,54,69,62,39,67,79,87,98}
In[]:=
Transpose@Prepend[Sort[Flatten/@E14],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 8 | 8 | 9 |
y | 2 | 6 | 3 | 9 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 7 | 9 | 9 | 2 | 7 | 8 |
w | 2 | 12 | 1 | 5 | -3 | 4 | 2 | 10 | 10 | 10 | 12 | 10 | -5 | 1 | 1 | 7 | -7 |
In[]:=
DijkstraPathsTree[ME14,5,SelectionRuleFirst,VertexCoordinatesEllipse[6,3],TableBreaks->{5}]
Vertex | 5 | 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 |
Distance | 0 | 10 | 3 | 4 | 10 | 14 | 9 | 2 | 9 |
From Vertex | -- | 5 | 8 | 2 | 5 | 4 | 8 | 9 | 6 |
{{23,1},{46,4},{51,10},{54,10},{69,-5},{82,1},{87,7},{98,-7}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 51 | 10 | 0 | ∞ | 10 | 51 | 1 |
{5,1} | 52 | 10 | 0 | ∞ | 10 | 52 | 2 |
{5,1,2} | 54 | 10 | 0 | ∞ | 10 | 54 | 4 |
{1,2,4} | 12 | 2 | 10 | 10 | 12 | ||
{1,2,4} | 16 | 12 | 10 | ∞ | 22 | 16 | 6 |
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{2,4,6} | 23 | 1 | 10 | ∞ | 11 | 23 | 3 |
{4,6,3} | 43 | -3 | 10 | 11 | 7 | 43 | 3 |
{4,6,3} | 46 | 4 | 10 | 22 | 14 | 46 | 6 |
{4,6,3} | 48 | 2 | 10 | ∞ | 12 | 48 | 8 |
{6,3,8} | 62 | 12 | 14 | 10 | 26 | ||
{6,3,8} | 67 | 10 | 14 | ∞ | 24 | 67 | 7 |
{6,3,8,7} | 69 | -5 | 14 | ∞ | 9 | 69 | 9 |
{3,8,7,9} | 39 | 5 | 7 | 9 | 12 | ||
{8,7,9} | 82 | 1 | 12 | 10 | 13 | ||
{8,7,9} | 87 | 7 | 12 | 24 | 19 | 87 | 7 |
{7,9} | 79 | 1 | 19 | 9 | 20 | ||
{9} | 98 | -7 | 9 | 12 | 2 | 98 | 8 |
{8} | 82 | 1 | 2 | 10 | 3 | 82 | 2 |
{8,2} | 87 | 7 | 2 | 19 | 9 | 87 | 7 |
{2,7} | 23 | 1 | 3 | 7 | 4 | 23 | 3 |
{7,3} | 79 | 1 | 9 | 9 | 10 | ||
{3} | 39 | 5 | 4 | 9 | 9 |
Example 24
Example 24
In[]:=
E15={{15,6},{19,4},{16,5},{29,4},{37,2},{36,8},{32,5},{35,5},{46,2},{42,5},{56,4},{76,2},{81,1},{85,4},{87,7},{94,1},{93,2}};ME15=ToWeightedAdjacencyMatrix[E15];gE15=ToEdgeWeightedGraph[E15];ME15//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 6 | 5 | ∞ | ∞ | 4 |
∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 4 |
∞ | 5 | 0 | ∞ | 5 | 8 | 2 | ∞ | ∞ |
∞ | 5 | ∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 2 | 0 | ∞ | ∞ |
1 | ∞ | ∞ | ∞ | 4 | ∞ | 7 | 0 | ∞ |
∞ | ∞ | 2 | 1 | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E15/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 7 | 8 | 8 | 8 | 9 | 9 |
y | 5 | 6 | 9 | 9 | 2 | 5 | 6 | 7 | 2 | 6 | 6 | 6 | 1 | 5 | 7 | 3 | 4 |
w | 6 | 5 | 4 | 4 | 5 | 5 | 8 | 2 | 5 | 2 | 4 | 2 | 1 | 4 | 7 | 2 | 1 |
In[]:=
DijkstraPathsTree[gE15,8,TraceFalse,VertexCoordinatesEllipse[6,3]];
Vertex | 8 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 9 |
Distance | 0 | 1 | 11 | 7 | 6 | 4 | 6 | 7 | 5 |
From Vertex | -- | 8 | 4 | 9 | 9 | 8 | 1 | 8 | 1 |
{{16,5},{19,4},{42,5},{81,1},{85,4},{87,7},{93,2},{94,1}}
In[]:=
DijkstraPathsTree[gE15,8,DistanceTableFalse,TraceTrue];
{{16,5},{19,4},{42,5},{81,1},{85,4},{87,7},{93,2},{94,1}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{8} | 81 | 1 | 0 | ∞ | 1 | 81 | 1 |
{8,1} | 85 | 4 | 0 | ∞ | 4 | 85 | 5 |
{8,1,5} | 87 | 7 | 0 | ∞ | 7 | 87 | 7 |
{1,5,7} | 15 | 6 | 1 | 4 | 7 | ||
{1,5,7} | 19 | 4 | 1 | ∞ | 5 | 19 | 9 |
{1,5,7,9} | 16 | 5 | 1 | ∞ | 6 | 16 | 6 |
{5,7,9,6} | 56 | 4 | 4 | 6 | 8 | ||
{7,9,6} | 76 | 2 | 7 | 6 | 9 | ||
{9,6} | 94 | 1 | 5 | ∞ | 6 | 94 | 4 |
{9,6,4} | 93 | 2 | 5 | ∞ | 7 | 93 | 3 |
{4,3} | 46 | 2 | 6 | 6 | 8 | ||
{4,3} | 42 | 5 | 6 | ∞ | 11 | 42 | 2 |
{3,2} | 37 | 2 | 7 | 7 | 9 | ||
{3,2} | 36 | 8 | 7 | 6 | 15 | ||
{3,2} | 32 | 5 | 7 | 11 | 12 | ||
{3,2} | 35 | 5 | 7 | 4 | 12 | ||
{2} | 29 | 4 | 11 | 5 | 15 |
Example 25
Example 25
In[]:=
E16={{18,4},{16,5},{14,3},{24,2},{38,7},{36,7},{45,4},{47,4},{58,2},{62,1},{65,3},{63,1},{74,5},{81,5},{82,1}};ME16=ToWeightedAdjacencyMatrix[E16];gE16=ToEdgeWeightedGraph[E16];ME16//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | 3 | ∞ | 5 | ∞ | 4 |
∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | 7 | ∞ | 7 |
∞ | ∞ | ∞ | 0 | 4 | ∞ | 4 | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | 2 |
∞ | 1 | 1 | ∞ | 3 | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | 5 | ∞ | ∞ | 0 | ∞ |
5 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E16/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 4 | 4 | 5 | 6 | 6 | 6 | 7 | 8 | 8 |
y | 4 | 6 | 8 | 4 | 6 | 8 | 5 | 7 | 8 | 2 | 3 | 5 | 4 | 1 | 2 |
w | 3 | 5 | 4 | 2 | 7 | 7 | 4 | 4 | 2 | 1 | 1 | 3 | 5 | 5 | 1 |
In[]:=
DijkstraPathsTree[E16,5,ImageSize{400,150},Spacings{0.5,1},TableBreaks{10},TraceTrue]
Vertex | 5 | 1 | 2 | 3 | 4 | 6 | 7 | 8 |
Distance | 0 | 7 | 3 | 13 | 5 | 12 | 9 | 2 |
From Vertex | -- | 8 | 8 | 6 | 2 | 1 | 4 | 5 |
{{16,5},{24,2},{47,4},{58,2},{63,1},{81,5},{82,1}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 58 | 2 | 0 | ∞ | 2 | 58 | 8 |
{8} | 81 | 5 | 2 | ∞ | 7 | 81 | 1 |
{8,1} | 82 | 1 | 2 | ∞ | 3 | 82 | 2 |
{1,2} | 18 | 4 | 7 | 2 | 11 | ||
{1,2} | 16 | 5 | 7 | ∞ | 12 | 16 | 6 |
{1,2,6} | 14 | 3 | 7 | ∞ | 10 | 14 | 4 |
{2,6,4} | 24 | 2 | 3 | 10 | 5 | 24 | 4 |
{6,4} | 62 | 1 | 12 | 3 | 13 | ||
{6,4} | 65 | 3 | 12 | 0 | 15 | ||
{6,4} | 63 | 1 | 12 | ∞ | 13 | 63 | 3 |
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{4,3} | 45 | 4 | 5 | 0 | 9 | ||
{4,3} | 47 | 4 | 5 | ∞ | 9 | 47 | 7 |
{3,7} | 38 | 7 | 13 | 2 | 20 | ||
{3,7} | 36 | 7 | 13 | 12 | 20 | ||
{7} | 74 | 5 | 9 | 5 | 14 |
In[]:=
DijkstraPathsTree[ME16,5,ImageSize{450,150},ShowTreeTrue,TraceReduce];
Vertex | 5 | 1 | 2 | 3 | 4 | 6 | 7 | 8 |
Distance | 0 | 7 | 3 | 13 | 5 | 12 | 9 | 2 |
From Vertex | -- | 8 | 8 | 6 | 2 | 1 | 4 | 5 |
{{16,5},{24,2},{47,4},{58,2},{63,1},{81,5},{82,1}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 58 | 2 | 0 | ∞ | 2 | 58 | 8 |
{8} | 81 | 5 | 2 | ∞ | 7 | 81 | 1 |
{8,1} | 82 | 1 | 2 | ∞ | 3 | 82 | 2 |
{1,2} | 14 | 3 | 7 | ∞ | 10 | 14 | 4 |
{1,2,4} | 16 | 5 | 7 | ∞ | 12 | 16 | 6 |
{2,4,6} | 24 | 2 | 3 | 10 | 5 | 24 | 4 |
{4,6} | 47 | 4 | 5 | ∞ | 9 | 47 | 7 |
{6,7} | 63 | 1 | 12 | ∞ | 13 | 63 | 3 |
Example 26
Example 26
In[]:=
E17={{15,0},{16,4},{17,7},{19,5},{23,7},{28,5},{31,7},{32,6},{35,2},{36,3},{39,4},{46,5},{47,3},{53,4},{58,1},{59,1},{64,7},{69,7},{73,6},{92,5},{95,6},{97,6},{98,5}};ME17=ToWeightedAdjacencyMatrix[E17];gE17=ToEdgeWeightedGraph[E17,DirectedEdgesTrue];ME17//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 0 | 4 | 7 | ∞ | 5 |
∞ | 0 | 7 | ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
7 | 6 | 0 | ∞ | 2 | 3 | ∞ | ∞ | 4 |
∞ | ∞ | ∞ | 0 | ∞ | 5 | 3 | ∞ | ∞ |
∞ | ∞ | 4 | ∞ | 0 | ∞ | ∞ | 1 | 1 |
∞ | ∞ | ∞ | 7 | ∞ | 0 | ∞ | ∞ | 7 |
∞ | ∞ | 6 | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ |
∞ | 5 | ∞ | ∞ | 6 | ∞ | 6 | 5 | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E17/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 7 | 9 | 9 | 9 | 9 |
y | 5 | 6 | 7 | 9 | 3 | 8 | 1 | 2 | 5 | 6 | 9 | 6 | 7 | 3 | 8 | 9 | 4 | 9 | 3 | 2 | 5 | 7 | 8 |
w | 0 | 4 | 7 | 5 | 7 | 5 | 7 | 6 | 2 | 3 | 4 | 5 | 3 | 4 | 1 | 1 | 7 | 7 | 6 | 5 | 6 | 6 | 5 |
In[]:=
DijkstraPathsTree[gE17,5,ShowTreeFalse,Spacings{0.5,1},TableBreaks8];
Vertex | 5 | 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 |
Distance | 0 | 11 | 6 | 4 | 14 | 7 | 7 | 1 | 1 |
From Vertex | -- | 3 | 9 | 5 | 6 | 3 | 9 | 5 | 5 |
{{31,7},{36,3},{53,4},{58,1},{59,1},{64,7},{92,5},{97,6}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 53 | 4 | 0 | ∞ | 4 | 53 | 3 |
{5,3} | 58 | 1 | 0 | ∞ | 1 | 58 | 8 |
{5,3,8} | 59 | 1 | 0 | ∞ | 1 | 59 | 9 |
{3,8,9} | 31 | 7 | 4 | ∞ | 11 | 31 | 1 |
{3,8,9,1} | 32 | 6 | 4 | ∞ | 10 | 32 | 2 |
{3,8,9,1,2} | 35 | 2 | 4 | 0 | 6 | ||
{3,8,9,1,2} | 36 | 3 | 4 | ∞ | 7 | 36 | 6 |
{3,8,9,1,2,6} | 39 | 4 | 4 | 1 | 8 |
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{9,1,2,6} | 92 | 5 | 1 | 10 | 6 | 92 | 2 |
{9,1,2,6} | 95 | 6 | 1 | 0 | 7 | ||
{9,1,2,6} | 97 | 6 | 1 | ∞ | 7 | 97 | 7 |
{9,1,2,6,7} | 98 | 5 | 1 | 1 | 6 | ||
{1,2,6,7} | 15 | 0 | 11 | 0 | 11 | ||
{1,2,6,7} | 16 | 4 | 11 | 7 | 15 | ||
{1,2,6,7} | 17 | 7 | 11 | 7 | 18 | ||
{1,2,6,7} | 19 | 5 | 11 | 1 | 16 |
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{2,6,7} | 23 | 7 | 6 | 4 | 13 | ||
{2,6,7} | 28 | 5 | 6 | 1 | 11 | ||
{6,7} | 64 | 7 | 7 | ∞ | 14 | 64 | 4 |
{6,7,4} | 69 | 7 | 7 | 1 | 14 | ||
{7,4} | 73 | 6 | 7 | 4 | 13 | ||
{4} | 46 | 5 | 14 | 7 | 19 | ||
{4} | 47 | 3 | 14 | 7 | 17 |
In[]:=
DijkstraPathsTree[E17,5,DistanceTableFalse,Spacings{0.5,1},ShowTreeFalse,TraceReduce]
{{31,7},{36,3},{53,4},{58,1},{59,1},{64,7},{92,5},{97,6}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 53 | 4 | 0 | ∞ | 4 | 53 | 3 |
{5,3} | 58 | 1 | 0 | ∞ | 1 | 58 | 8 |
{5,3,8} | 59 | 1 | 0 | ∞ | 1 | 59 | 9 |
{3,8,9} | 31 | 7 | 4 | ∞ | 11 | 31 | 1 |
{3,8,9,1} | 32 | 6 | 4 | ∞ | 10 | 32 | 2 |
{3,8,9,1,2} | 36 | 3 | 4 | ∞ | 7 | 36 | 6 |
{9,1,2,6} | 92 | 5 | 1 | 10 | 6 | 92 | 2 |
{9,1,2,6} | 97 | 6 | 1 | ∞ | 7 | 97 | 7 |
{6,7} | 64 | 7 | 7 | ∞ | 14 | 64 | 4 |
Example 27
Example 27
In[]:=
E18={{13,5},{24,5},{27,1},{28,3},{29,0},{35,3},{36,5},{46,1},{47,4},{56,4},{57,3},{58,2},{59,4},{64,2},{67,0},{69,5},{71,4},{73,5},{74,7},{85,3},{91,6},{92,3},{94,5},{95,6},{98,1}};ME18=ToWeightedAdjacencyMatrix[E18];gE18=ToEdgeWeightedGraph[E18,DirectedEdgesTrue];ME18//MatrixForm
Out[]//MatrixForm=
0 | ∞ | 5 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | 5 | ∞ | ∞ | 1 | 3 | 0 |
∞ | ∞ | 0 | ∞ | 3 | 5 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | 0 | ∞ | 1 | 4 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | 3 | 2 | 4 |
∞ | ∞ | ∞ | 2 | ∞ | 0 | 0 | ∞ | 5 |
4 | ∞ | 5 | 7 | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 3 | ∞ | ∞ | 0 | ∞ |
6 | 3 | ∞ | 5 | 6 | ∞ | ∞ | 1 | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E18/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 2 | 2 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 7 | 8 | 9 | 9 | 9 | 9 | 9 |
y | 3 | 4 | 7 | 8 | 9 | 5 | 6 | 6 | 7 | 6 | 7 | 8 | 9 | 4 | 7 | 9 | 1 | 3 | 4 | 5 | 1 | 2 | 4 | 5 | 8 |
w | 5 | 5 | 1 | 3 | 0 | 3 | 5 | 1 | 4 | 4 | 3 | 2 | 4 | 2 | 0 | 5 | 4 | 5 | 7 | 3 | 6 | 3 | 5 | 6 | 1 |
In[]:=
DijkstraPathsTree[E18,5,TraceReduce];
Vertex | 5 | 1 | 2 | 3 | 4 | 6 | 7 | 8 | 9 |
Distance | 0 | 7 | 7 | 8 | 6 | 4 | 3 | 2 | 4 |
From Vertex | -- | 7 | 9 | 7 | 6 | 5 | 5 | 5 | 5 |
{{56,4},{57,3},{58,2},{59,4},{64,2},{71,4},{73,5},{92,3}}
M | x→y | w | U(x) | U(y) | U(x)+w | FromWhere | y |
{5} | 56 | 4 | 0 | ∞ | 4 | 56 | 6 |
{5,6} | 57 | 3 | 0 | ∞ | 3 | 57 | 7 |
{5,6,7} | 58 | 2 | 0 | ∞ | 2 | 58 | 8 |
{5,6,7,8} | 59 | 4 | 0 | ∞ | 4 | 59 | 9 |
{6,7,8,9} | 64 | 2 | 4 | ∞ | 6 | 64 | 4 |
{7,8,9,4} | 71 | 4 | 3 | ∞ | 7 | 71 | 1 |
{7,8,9,4,1} | 73 | 5 | 3 | ∞ | 8 | 73 | 3 |
{9,4,1,3} | 92 | 3 | 4 | ∞ | 7 | 92 | 2 |
In[]:=
DijkstraPathsTree[gE18,6,TraceFalse,TreeFalse]
Vertex | 6 | 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
Distance | 0 | 4 | 8 | 5 | 2 | 8 | 0 | 6 | 5 |
From Vertex | -- | 7 | 9 | 7 | 6 | 3 | 6 | 9 | 6 |
Shortest Paths: Floyd’s Algorithm
Shortest Paths: Floyd’s Algorithm
Options
Options
Alternatives for optional arguments : -- DistanceTable -> True | False,-- FontSize -> 10 | Any fontsize,-- Partition -> False | integer > 1,-- FromWhereTable -> False | True,-- Trace -> False | True.
In[]:=
Options[Floyd]
Out[]=
{DistanceTableTrue,FontSize10,FromWhereTableTrue,PartitionFalse,TraceFalse}
Example 22
Example 22
In[]:=
E19={{{1,2},2},{{1,6},12},{{5,2},17},{{2,3},1},{{2,8},1},{{3,1},-4},{{9,6},4},{{4,3},3},{{4,6},4},{{4,8},12},{{5,1},8},{{5,4},10},{{6,2},12},{{6,3},12},{{6,7},10},{{8,7},7}};ME19=ToWeightedAdjacencyMatrix[E19];gE19=ToEdgeWeightedGraph[E19];ME19//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 17 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@E19],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 8 | 9 |
y | 2 | 6 | 3 | 8 | 1 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 3 | 7 | 7 | 6 |
w | 2 | 12 | 1 | 1 | -4 | 3 | 4 | 12 | 8 | 17 | 10 | 12 | 12 | 10 | 7 | 4 |
In[]:=
ToGraph[E19,GraphLayoutAutomatic,ImagePadding10,VertexCoordinates{{0.9191450300180578`,1.0504514628777804`},{1.0504514628777804`,0.5252257314388902`},{0.`,0.5252257314388902`},{0.`,0.`},{0.5252257314388902`,0.5252257314388902`-(1/5)},{1.8382900600361156`,0.5252257314388902`},{2.1009029257555607`,0.`},{1.0504514628777804`,0.`},{1.5756771943166705`,0.`}},VertexLabels"Name",VertexStyleRed]
Out[]=
In[]:=
Floyd[E19]
Out[]=
Found cycle of negative weight |
{{31,-4},{12,2},{23,1}} |
In[]:=
Floyd[ME19,TraceTrue]
Found cycle of negative weight |
{{31,-4},{12,2},{23,1}} |
Out[]=
,
,
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 17 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | -2 | 0 | ∞ | ∞ | 8 | ∞ | ∞ | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 10 | ∞ | 10 | 0 | 20 | ∞ | ∞ | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | ∞ | 3 | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | -2 | -1 | ∞ | ∞ | 8 | ∞ | -1 | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 10 | 11 | 10 | 0 | 20 | ∞ | 11 | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | 13 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
Example 23
Example 23
In[]:=
E20={{{1,2},2},{{1,6},12},{{5,2},10},{{2,3},1},{{8,2},1},{{4,3},-3},{{4,6},4},{{4,8},2},{{5,1},10},{{5,4},10},{{6,9},-5},{{6,2},12},{{3,9},5},{{6,7},10},{{7,9},1},{{8,7},7},{{9,8},-7}};ME20=ToWeightedAdjacencyMatrix[E20];gE20=ToEdgeWeightedGraph[E20];ME20//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | ∞ | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@E20],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 8 | 8 | 9 |
y | 2 | 6 | 3 | 9 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 7 | 9 | 9 | 2 | 7 | 8 |
w | 2 | 12 | 1 | 5 | -3 | 4 | 2 | 10 | 10 | 10 | 12 | 10 | -5 | 1 | 1 | 7 | -7 |
In[]:=
Graph[EdgeList[gE20],ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
Floyd[ME20,Partition2,TraceTrue]//Row[#,", "]&/@#&//StylePrint/@#&;
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | 0 | 1 | 2 | ∞ | ∞ | 12 | 7 | 0 | 7 |
2 | ∞ | 0 | 1 | ∞ | ∞ | ∞ | 6 | -1 | 6 |
3 | ∞ | -1 | 0 | ∞ | ∞ | ∞ | 5 | -2 | 5 |
4 | ∞ | -7 | -6 | 0 | ∞ | 4 | -1 | -8 | -1 |
5 | 10 | 3 | 4 | 10 | 0 | 14 | 9 | 2 | 9 |
6 | ∞ | -11 | -10 | ∞ | ∞ | 0 | -5 | -12 | -5 |
7 | ∞ | -5 | -4 | ∞ | ∞ | ∞ | 0 | -6 | 1 |
8 | ∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
9 | ∞ | -6 | -5 | ∞ | ∞ | ∞ | 0 | -7 | 0 |
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | ★ | 82 | 23 | ★ | ★ | 16 | 87 | 98 | 69 |
2 | ★ | ★ | 23 | ★ | ★ | ★ | 87 | 98 | 39 |
3 | ★ | 82 | ★ | ★ | ★ | ★ | 87 | 98 | 39 |
4 | ★ | 82 | 23 | ★ | ★ | 46 | 87 | 98 | 69 |
5 | 51 | 82 | 23 | 54 | ★ | 46 | 87 | 98 | 69 |
6 | ★ | 82 | 23 | ★ | ★ | ★ | 87 | 98 | 69 |
7 | ★ | 82 | 23 | ★ | ★ | ★ | ★ | 98 | 79 |
8 | ★ | 82 | 23 | ★ | ★ | ★ | 87 | ★ | 39 |
9 | ★ | 82 | 23 | ★ | ★ | ★ | 87 | 98 | ★ |
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | ∞ | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | ∞ | 10 | 0 | 22 | ∞ | ∞ | ∞ |
∞ | 12 | ∞ | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | 11 | 10 | 0 | 22 | ∞ | ∞ | ∞ |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | ∞ | ∞ | 8 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | 2 |
10 | 10 | 11 | 10 | 0 | 22 | ∞ | ∞ | 16 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | ∞ | ∞ | 8 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | 2 |
10 | 10 | 7 | 10 | 0 | 14 | ∞ | 12 | 12 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | ∞ | ∞ | 8 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | 2 |
10 | 10 | 7 | 10 | 0 | 14 | ∞ | 12 | 12 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | 22 | ∞ | 7 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | 16 | -3 | 0 | ∞ | 4 | 14 | 2 | -1 |
10 | 10 | 7 | 10 | 0 | 14 | 24 | 12 | 9 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | 22 | ∞ | 7 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | 16 | -3 | 0 | ∞ | 4 | 14 | 2 | -1 |
10 | 10 | 7 | 10 | 0 | 14 | 24 | 12 | 9 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
0 | 2 | 3 | ∞ | ∞ | 12 | 22 | ∞ | 7 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 6 |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | 3 | -3 | 0 | ∞ | 4 | 9 | 2 | -1 |
10 | 10 | 7 | 10 | 0 | 14 | 19 | 12 | 9 |
∞ | 12 | 13 | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | -6 | -5 | ∞ | ∞ | ∞ | 0 | -7 | 0 |
0 | 1 | 2 | ∞ | ∞ | 12 | 7 | 0 | 7 |
∞ | 0 | 1 | ∞ | ∞ | ∞ | 6 | -1 | 6 |
∞ | -1 | 0 | ∞ | ∞ | ∞ | 5 | -2 | 5 |
∞ | -7 | -6 | 0 | ∞ | 4 | -1 | -8 | -1 |
10 | 3 | 4 | 10 | 0 | 14 | 9 | 2 | 9 |
∞ | -11 | -10 | ∞ | ∞ | 0 | -5 | -12 | -5 |
∞ | -5 | -4 | ∞ | ∞ | ∞ | 0 | -6 | 1 |
∞ | 1 | 2 | ∞ | ∞ | ∞ | 7 | 0 | 7 |
∞ | -6 | -5 | ∞ | ∞ | ∞ | 0 | -7 | 0 |
Example 24
Example 24
In[]:=
E21={{15,6},{19,4},{16,5},{29,4},{37,2},{36,8},{32,5},{35,5},{46,2},{42,5},{56,4},{76,2},{81,1},{85,4},{87,7},{94,1},{93,2}};ME21=ToWeightedAdjacencyMatrix[E21];gE21=ToEdgeWeightedGraph[E21];ME21//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 6 | 5 | ∞ | ∞ | 4 |
∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 4 |
∞ | 5 | 0 | ∞ | 5 | 8 | 2 | ∞ | ∞ |
∞ | 5 | ∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 2 | 0 | ∞ | ∞ |
1 | ∞ | ∞ | ∞ | 4 | ∞ | 7 | 0 | ∞ |
∞ | ∞ | 2 | 1 | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E21/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 7 | 8 | 8 | 8 | 9 | 9 |
y | 5 | 6 | 9 | 9 | 2 | 5 | 6 | 7 | 2 | 6 | 6 | 6 | 1 | 5 | 7 | 3 | 4 |
w | 6 | 5 | 4 | 4 | 5 | 5 | 8 | 2 | 5 | 2 | 4 | 2 | 1 | 4 | 7 | 2 | 1 |
In[]:=
Graph[EdgeList[gE21],ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
Floyd[gE21,FromWhereTableFalse]
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | 0 | 10 | 6 | 5 | 6 | 5 | 8 | ∞ | 4 |
2 | ∞ | 0 | 6 | 5 | 11 | 7 | 8 | ∞ | 4 |
3 | ∞ | 5 | 0 | 10 | 5 | 4 | 2 | ∞ | 9 |
4 | ∞ | 5 | 11 | 0 | 16 | 2 | 13 | ∞ | 9 |
5 | ∞ | ∞ | ∞ | ∞ | 0 | 4 | ∞ | ∞ | ∞ |
6 | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | ∞ |
7 | ∞ | ∞ | ∞ | ∞ | ∞ | 2 | 0 | ∞ | ∞ |
8 | 1 | 11 | 7 | 6 | 4 | 6 | 7 | 0 | 5 |
9 | ∞ | 6 | 2 | 1 | 7 | 3 | 4 | ∞ | 0 |
Example 25
Example 25
In[]:=
E22={{18,4},{16,5},{14,3},{24,2},{38,7},{36,7},{45,4},{47,4},{58,2},{62,1},{65,3},{63,1},{74,5},{81,5},{82,1}};ME22=ToWeightedAdjacencyMatrix[E22];gE22=ToEdgeWeightedGraph[E22];ME22//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | 3 | ∞ | 5 | ∞ | 4 |
∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | 7 | ∞ | 7 |
∞ | ∞ | ∞ | 0 | 4 | ∞ | 4 | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | 2 |
∞ | 1 | 1 | ∞ | 3 | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | 5 | ∞ | ∞ | 0 | ∞ |
5 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E22/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 4 | 4 | 5 | 6 | 6 | 6 | 7 | 8 | 8 |
y | 4 | 6 | 8 | 4 | 6 | 8 | 5 | 7 | 8 | 2 | 3 | 5 | 4 | 1 | 2 |
w | 3 | 5 | 4 | 2 | 7 | 7 | 4 | 4 | 2 | 1 | 1 | 3 | 5 | 5 | 1 |
In[]:=
Graph[EdgeList[gE22],ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
Floyd[gE22,DistanceTableFalse]
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
1 | ★ | 82 | 63 | 14 | 45 | 16 | 47 | 18 |
2 | 81 | ★ | 63 | 24 | 45 | 16 | 47 | 58 |
3 | 81 | 62 | ★ | 24 | 65 | 36 | 47 | 38 |
4 | 81 | 82 | 63 | ★ | 45 | 16 | 47 | 58 |
5 | 81 | 82 | 63 | 24 | ★ | 16 | 47 | 58 |
6 | 81 | 62 | 63 | 24 | 65 | ★ | 47 | 58 |
7 | 81 | 82 | 63 | 74 | 45 | 16 | ★ | 58 |
8 | 81 | 82 | 63 | 24 | 45 | 16 | 47 | ★ |
Example 26
Example 26
In[]:=
E23={{15,0},{16,4},{17,7},{19,5},{23,7},{28,5},{31,7},{32,6},{35,2},{36,3},{39,4},{46,5},{47,3},{53,4},{58,1},{59,1},{64,7},{69,7},{73,6},{92,5},{95,6},{97,6},{98,5}};ME23=ToWeightedAdjacencyMatrix[E23];gE23=ToEdgeWeightedGraph[E23,DirectedEdgesTrue];ME23//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 0 | 4 | 7 | ∞ | 5 |
∞ | 0 | 7 | ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
7 | 6 | 0 | ∞ | 2 | 3 | ∞ | ∞ | 4 |
∞ | ∞ | ∞ | 0 | ∞ | 5 | 3 | ∞ | ∞ |
∞ | ∞ | 4 | ∞ | 0 | ∞ | ∞ | 1 | 1 |
∞ | ∞ | ∞ | 7 | ∞ | 0 | ∞ | ∞ | 7 |
∞ | ∞ | 6 | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ |
∞ | 5 | ∞ | ∞ | 6 | ∞ | 6 | 5 | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E23/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 7 | 9 | 9 | 9 | 9 |
y | 5 | 6 | 7 | 9 | 3 | 8 | 1 | 2 | 5 | 6 | 9 | 6 | 7 | 3 | 8 | 9 | 4 | 9 | 3 | 2 | 5 | 7 | 8 |
w | 0 | 4 | 7 | 5 | 7 | 5 | 7 | 6 | 2 | 3 | 4 | 5 | 3 | 4 | 1 | 1 | 7 | 7 | 6 | 5 | 6 | 6 | 5 |
In[]:=
Graph[EdgeList[gE23],ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
Floyd[gE23,FromWhereTableFalse,TraceFalse]
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | 0 | 6 | 4 | 11 | 0 | 4 | 7 | 1 | 1 |
2 | 14 | 0 | 7 | 17 | 9 | 10 | 16 | 5 | 10 |
3 | 7 | 6 | 0 | 10 | 2 | 3 | 9 | 3 | 3 |
4 | 16 | 15 | 9 | 0 | 11 | 5 | 3 | 12 | 12 |
5 | 11 | 6 | 4 | 14 | 0 | 7 | 7 | 1 | 1 |
6 | 23 | 12 | 16 | 7 | 13 | 0 | 10 | 12 | 7 |
7 | 13 | 12 | 6 | 16 | 8 | 9 | 0 | 9 | 9 |
8 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ |
9 | 17 | 5 | 10 | 20 | 6 | 13 | 6 | 5 | 0 |
Example 27
Example 27
In[]:=
E24={{13,5},{24,5},{27,1},{28,3},{29,0},{35,3},{36,5},{46,1},{47,4},{56,4},{57,3},{58,2},{59,4},{64,2},{67,0},{69,5},{71,4},{73,5},{74,7},{85,3},{91,6},{92,3},{94,5},{95,6},{98,1}};ME24=ToWeightedAdjacencyMatrix[E24];gE24=ToEdgeWeightedGraph[E24,DirectedEdgesTrue];ME24//MatrixForm
Out[]//MatrixForm=
0 | ∞ | 5 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | 5 | ∞ | ∞ | 1 | 3 | 0 |
∞ | ∞ | 0 | ∞ | 3 | 5 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | 0 | ∞ | 1 | 4 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | 3 | 2 | 4 |
∞ | ∞ | ∞ | 2 | ∞ | 0 | 0 | ∞ | 5 |
4 | ∞ | 5 | 7 | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 3 | ∞ | ∞ | 0 | ∞ |
6 | 3 | ∞ | 5 | 6 | ∞ | ∞ | 1 | 0 |
In[]:=
Transpose@Prepend[Sort[Map[Flatten,E24/.EdgeToPair]],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 2 | 2 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 7 | 8 | 9 | 9 | 9 | 9 | 9 |
y | 3 | 4 | 7 | 8 | 9 | 5 | 6 | 6 | 7 | 6 | 7 | 8 | 9 | 4 | 7 | 9 | 1 | 3 | 4 | 5 | 1 | 2 | 4 | 5 | 8 |
w | 5 | 5 | 1 | 3 | 0 | 3 | 5 | 1 | 4 | 4 | 3 | 2 | 4 | 2 | 0 | 5 | 4 | 5 | 7 | 3 | 6 | 3 | 5 | 6 | 1 |
In[]:=
Graph[EdgeList[gE24],ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
Floyd[gE24,DistanceTableTrue,FromWhereTableFalse]
★ | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
1 | 0 | 15 | 5 | 12 | 8 | 10 | 10 | 10 | 12 |
2 | 5 | 0 | 6 | 5 | 4 | 6 | 1 | 1 | 0 |
3 | 9 | 10 | 0 | 7 | 3 | 5 | 5 | 5 | 7 |
4 | 5 | 9 | 6 | 0 | 9 | 1 | 1 | 7 | 6 |
5 | 7 | 7 | 8 | 6 | 0 | 4 | 3 | 2 | 4 |
6 | 4 | 8 | 5 | 2 | 8 | 0 | 0 | 6 | 5 |
7 | 4 | 15 | 5 | 7 | 8 | 8 | 0 | 10 | 12 |
8 | 10 | 10 | 11 | 9 | 3 | 7 | 6 | 0 | 7 |
9 | 6 | 3 | 9 | 5 | 4 | 6 | 4 | 1 | 0 |
Shortest Paths: FloydShortestPaths
Shortest Paths: FloydShortestPaths
Options
Options
Alternatives for optional arguments: -- FromWhere -> All | any part specification of the list.
In[]:=
Options[FloydShortestPaths]
Out[]=
{FramedTrue,FromWhereAll,PrintTrue}
Example 28
Example 28
In[]:=
E25={{{1,2},2},{{1,6},12},{{5,2},17},{{2,3},1},{{2,8},1},{{3,1},-4},{{9,6},4},{{4,3},3},{{4,6},4},{{4,8},12},{{5,1},8},{{5,4},10},{{6,2},12},{{6,3},12},{{6,7},10},{{8,7},7}};ME25=ToWeightedAdjacencyMatrix[E25];gE25=ToEdgeWeightedGraph[E25];ME25//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | 1 | ∞ |
-4 | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 3 | 0 | ∞ | 4 | ∞ | 12 | ∞ |
8 | 17 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | 12 | ∞ | ∞ | 0 | 10 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 4 | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@E25],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 8 | 9 |
y | 2 | 6 | 3 | 8 | 1 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 3 | 7 | 7 | 6 |
w | 2 | 12 | 1 | 1 | -4 | 3 | 4 | 12 | 8 | 17 | 10 | 12 | 12 | 10 | 7 | 4 |
In[]:=
FloydShortestPaths[E25]
Out[]=
Found cycle of negative weight |
{{31,-4},{12,2},{23,1}} |
Example 29
Example 29
In[]:=
E26={{{1,2},2},{{1,6},12},{{5,2},10},{{2,3},1},{{8,2},1},{{4,3},-3},{{4,6},4},{{4,8},2},{{5,1},10},{{5,4},10},{{6,9},-5},{{6,2},12},{{3,9},5},{{6,7},10},{{7,9},1},{{8,7},7},{{9,8},-7}};ME26=ToWeightedAdjacencyMatrix[E26];gE26=ToEdgeWeightedGraph[E26];ME26//MatrixForm
Out[]//MatrixForm=
0 | 2 | ∞ | ∞ | ∞ | 12 | ∞ | ∞ | ∞ |
∞ | 0 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | 5 |
∞ | ∞ | -3 | 0 | ∞ | 4 | ∞ | 2 | ∞ |
10 | 10 | ∞ | 10 | 0 | ∞ | ∞ | ∞ | ∞ |
∞ | 12 | ∞ | ∞ | ∞ | 0 | 10 | ∞ | -5 |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | 1 |
∞ | 1 | ∞ | ∞ | ∞ | ∞ | 7 | 0 | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | -7 | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@E26],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 2 | 3 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 8 | 8 | 9 |
y | 2 | 6 | 3 | 9 | 3 | 6 | 8 | 1 | 2 | 4 | 2 | 7 | 9 | 9 | 2 | 7 | 8 |
w | 2 | 12 | 1 | 5 | -3 | 4 | 2 | 10 | 10 | 10 | 12 | 10 | -5 | 1 | 1 | 7 | -7 |
In[]:=
FloydShortestPaths[ME26,FromWhere{1}]
{{16,12}} |
{{16,12},{69,-5}} |
{{16,12},{69,-5},{98,-7}} |
{{16,12},{69,-5},{98,-7},{82,1}} |
{{16,12},{69,-5},{98,-7},{87,7}} |
{{16,12},{69,-5},{98,-7},{82,1},{23,1}} |
In[]:=
FloydShortestPaths[ME26,FromWhere3;;5]
{{39,5}} |
{{39,5},{98,-7}} |
{{39,5},{98,-7},{82,1}} |
{{39,5},{98,-7},{87,7}} |
{{46,4}} |
{{46,4},{69,-5}} |
{{46,4},{69,-5},{98,-7}} |
{{46,4},{69,-5},{98,-7},{82,1}} |
{{46,4},{69,-5},{98,-7},{87,7}} |
{{46,4},{69,-5},{98,-7},{82,1},{23,1}} |
{{51,10}} |
{{54,10}} |
{{54,10},{46,4}} |
{{54,10},{46,4},{69,-5}} |
{{54,10},{46,4},{69,-5},{98,-7}} |
{{54,10},{46,4},{69,-5},{98,-7},{82,1}} |
{{54,10},{46,4},{69,-5},{98,-7},{87,7}} |
{{54,10},{46,4},{69,-5},{98,-7},{82,1},{23,1}} |
In[]:=
FloydShortestPaths[ME26,FromWhere;;2]
{{16,12}} |
{{16,12},{69,-5}} |
{{16,12},{69,-5},{98,-7}} |
{{16,12},{69,-5},{98,-7},{82,1}} |
{{16,12},{69,-5},{98,-7},{87,7}} |
{{16,12},{69,-5},{98,-7},{82,1},{23,1}} |
{{23,1}} |
{{23,1},{39,5}} |
{{23,1},{39,5},{98,-7}} |
{{23,1},{39,5},{98,-7},{87,7}} |
In[]:=
FloydShortestPaths[ME26,FromWhere{3,6}]
{{39,5}} |
{{39,5},{98,-7}} |
{{39,5},{98,-7},{82,1}} |
{{39,5},{98,-7},{87,7}} |
{{69,-5}} |
{{69,-5},{98,-7}} |
{{69,-5},{98,-7},{82,1}} |
{{69,-5},{98,-7},{87,7}} |
{{69,-5},{98,-7},{82,1},{23,1}} |
Example 30
Example 30
In[]:=
E27={{15,6},{19,4},{16,5},{29,4},{37,2},{36,8},{32,5},{35,5},{46,2},{42,5},{56,4},{76,2},{81,1},{85,4},{87,7},{94,1},{93,2}};ME27=ToWeightedAdjacencyMatrix[E27];gE27=ToEdgeWeightedGraph[E27];ME27//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 6 | 5 | ∞ | ∞ | 4 |
∞ | 0 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 4 |
∞ | 5 | 0 | ∞ | 5 | 8 | 2 | ∞ | ∞ |
∞ | 5 | ∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | 2 | 0 | ∞ | ∞ |
1 | ∞ | ∞ | ∞ | 4 | ∞ | 7 | 0 | ∞ |
∞ | ∞ | 2 | 1 | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@(E27/.EdgeToPair)],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 7 | 8 | 8 | 8 | 9 | 9 |
y | 5 | 6 | 9 | 9 | 2 | 5 | 6 | 7 | 2 | 6 | 6 | 6 | 1 | 5 | 7 | 3 | 4 |
w | 6 | 5 | 4 | 4 | 5 | 5 | 8 | 2 | 5 | 2 | 4 | 2 | 1 | 4 | 7 | 2 | 1 |
In[]:=
FloydShortestPaths[gE27,FromWhere{1,5,8}]
{{15,6}} |
{{16,5}} |
{{19,4}} |
{{19,4},{93,2}} |
{{19,4},{94,1}} |
{{19,4},{93,2},{37,2}} |
{{19,4},{94,1},{42,5}} |
{{56,4}} |
{{81,1}} |
{{85,4}} |
{{87,7}} |
{{81,1},{16,5}} |
{{81,1},{19,4}} |
{{81,1},{19,4},{93,2}} |
{{81,1},{19,4},{94,1}} |
{{81,1},{19,4},{94,1},{42,5}} |
Example 31
Example 31
In[]:=
E28={{18,4},{16,5},{14,3},{24,2},{38,7},{36,7},{45,4},{47,4},{58,2},{62,1},{65,3},{63,1},{74,5},{81,5},{82,1}};ME28=ToWeightedAdjacencyMatrix[E28];gE28=ToEdgeWeightedGraph[E28];ME28//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | 3 | ∞ | 5 | ∞ | 4 |
∞ | 0 | ∞ | 2 | ∞ | ∞ | ∞ | ∞ |
∞ | ∞ | 0 | ∞ | ∞ | 7 | ∞ | 7 |
∞ | ∞ | ∞ | 0 | 4 | ∞ | 4 | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | ∞ | ∞ | 2 |
∞ | 1 | 1 | ∞ | 3 | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | 5 | ∞ | ∞ | 0 | ∞ |
5 | 1 | ∞ | ∞ | ∞ | ∞ | ∞ | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@(E28/.EdgeToPair)],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 2 | 3 | 3 | 4 | 4 | 5 | 6 | 6 | 6 | 7 | 8 | 8 |
y | 4 | 6 | 8 | 4 | 6 | 8 | 5 | 7 | 8 | 2 | 3 | 5 | 4 | 1 | 2 |
w | 3 | 5 | 4 | 2 | 7 | 7 | 4 | 4 | 2 | 1 | 1 | 3 | 5 | 5 | 1 |
In[]:=
FloydShortestPaths[ME28,FromWhere6;;7]
{{62,1}} |
{{63,1}} |
{{65,3}} |
{{62,1},{24,2}} |
{{65,3},{58,2}} |
{{62,1},{24,2},{47,4}} |
{{65,3},{58,2},{81,5}} |
{{74,5}} |
{{74,5},{45,4}} |
{{74,5},{45,4},{58,2}} |
{{74,5},{45,4},{58,2},{81,5}} |
{{74,5},{45,4},{58,2},{82,1}} |
{{74,5},{45,4},{58,2},{81,5},{16,5}} |
{{74,5},{45,4},{58,2},{81,5},{16,5},{63,1}} |
Example 32
Example 32
In[]:=
E29={{15,0},{16,4},{17,7},{19,5},{23,7},{28,5},{31,7},{32,6},{35,2},{36,3},{39,4},{46,5},{47,3},{53,4},{58,1},{59,1},{64,7},{69,7},{73,6},{92,5},{95,6},{97,6},{98,5}};ME29=ToWeightedAdjacencyMatrix[E29];gE29=ToEdgeWeightedGraph[E29,DirectedEdgesTrue];ME29//MatrixForm
Out[]//MatrixForm=
0 | ∞ | ∞ | ∞ | 0 | 4 | 7 | ∞ | 5 |
∞ | 0 | 7 | ∞ | ∞ | ∞ | ∞ | 5 | ∞ |
7 | 6 | 0 | ∞ | 2 | 3 | ∞ | ∞ | 4 |
∞ | ∞ | ∞ | 0 | ∞ | 5 | 3 | ∞ | ∞ |
∞ | ∞ | 4 | ∞ | 0 | ∞ | ∞ | 1 | 1 |
∞ | ∞ | ∞ | 7 | ∞ | 0 | ∞ | ∞ | 7 |
∞ | ∞ | 6 | ∞ | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ | 0 | ∞ |
∞ | 5 | ∞ | ∞ | 6 | ∞ | 6 | 5 | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@(E29/.EdgeToPair)],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 3 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 7 | 9 | 9 | 9 | 9 |
y | 5 | 6 | 7 | 9 | 3 | 8 | 1 | 2 | 5 | 6 | 9 | 6 | 7 | 3 | 8 | 9 | 4 | 9 | 3 | 2 | 5 | 7 | 8 |
w | 0 | 4 | 7 | 5 | 7 | 5 | 7 | 6 | 2 | 3 | 4 | 5 | 3 | 4 | 1 | 1 | 7 | 7 | 6 | 5 | 6 | 6 | 5 |
In[]:=
FloydShortestPaths[ME29,FromWhere6;;7]
{{64,7}} |
{{69,7}} |
{{64,7},{47,3}} |
{{69,7},{92,5}} |
{{69,7},{95,6}} |
{{69,7},{98,5}} |
{{64,7},{47,3},{73,6}} |
{{64,7},{47,3},{73,6},{31,7}} |
{{73,6}} |
{{73,6},{31,7}} |
{{73,6},{32,6}} |
{{73,6},{35,2}} |
{{73,6},{36,3}} |
{{73,6},{35,2},{58,1}} |
{{73,6},{35,2},{59,1}} |
{{73,6},{36,3},{64,7}} |
Example 33
Example 33
In[]:=
E30={{13,5},{24,5},{27,1},{28,3},{29,0},{35,3},{36,5},{46,1},{47,4},{56,4},{57,3},{58,2},{59,4},{64,2},{67,0},{69,5},{71,4},{73,5},{74,7},{85,3},{91,6},{92,3},{94,5},{95,6},{98,1}};ME30=ToWeightedAdjacencyMatrix[E30];gE30=ToEdgeWeightedGraph[E30,DirectedEdgesTrue];ME30//MatrixForm
Out[]//MatrixForm=
0 | ∞ | 5 | ∞ | ∞ | ∞ | ∞ | ∞ | ∞ |
∞ | 0 | ∞ | 5 | ∞ | ∞ | 1 | 3 | 0 |
∞ | ∞ | 0 | ∞ | 3 | 5 | ∞ | ∞ | ∞ |
∞ | ∞ | ∞ | 0 | ∞ | 1 | 4 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 0 | 4 | 3 | 2 | 4 |
∞ | ∞ | ∞ | 2 | ∞ | 0 | 0 | ∞ | 5 |
4 | ∞ | 5 | 7 | ∞ | ∞ | 0 | ∞ | ∞ |
∞ | ∞ | ∞ | ∞ | 3 | ∞ | ∞ | 0 | ∞ |
6 | 3 | ∞ | 5 | 6 | ∞ | ∞ | 1 | 0 |
In[]:=
Transpose@Prepend[Sort[Flatten/@(E30/.EdgeToPair)],{"x","y","w"}]//Grid[#,DividersAll]&
Out[]=
x | 1 | 2 | 2 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 5 | 6 | 6 | 6 | 7 | 7 | 7 | 8 | 9 | 9 | 9 | 9 | 9 |
y | 3 | 4 | 7 | 8 | 9 | 5 | 6 | 6 | 7 | 6 | 7 | 8 | 9 | 4 | 7 | 9 | 1 | 3 | 4 | 5 | 1 | 2 | 4 | 5 | 8 |
w | 5 | 5 | 1 | 3 | 0 | 3 | 5 | 1 | 4 | 4 | 3 | 2 | 4 | 2 | 0 | 5 | 4 | 5 | 7 | 3 | 6 | 3 | 5 | 6 | 1 |
In[]:=
FloydShortestPaths[ME30,FromWhere;;2]
{{13,5}} |
{{13,5},{35,3}} |
{{13,5},{36,5}} |
{{13,5},{35,3},{58,2}} |
{{13,5},{35,3},{59,4}} |
{{13,5},{36,5},{64,2}} |
{{13,5},{36,5},{67,0}} |
{{13,5},{35,3},{59,4},{92,3}} |
{{24,5}} |
{{27,1}} |
{{29,0}} |
{{24,5},{46,1}} |
{{27,1},{71,4}} |
{{27,1},{73,5}} |
{{29,0},{98,1}} |
{{29,0},{98,1},{85,3}} |
EulerTourQ and EulerTour
EulerTourQ and EulerTour
Alternatives for optional argument of EulerTourQ:-- Trace -> True | False
In[]:=
Options[EulerTourQ]
Out[]=
{TraceFalse}
Alternatives for optional arguments : -- FontSize -> 10 | any font size,-- GraphLayout Automatic | any Graph layout,-- ImagePadding 15,-- ImageSize Automatic | {450, 150} | any dimensions,-- ItemSize -> Automatic | {2, 4, 2, 2, 28, 2} | {s1, s2, s3, s4, s5, s6},-- ManipulateTour -> True |False,-- ManipulateWalk -> False | True,-- SelectionRule First | Last | RandomChoice,-- Spacings -> Automatic | {s1, Automatic} | {s1, s2},-- TableBreaks → False | integer | {integers},-- TourStyle -> {Automatic | Thick | Thin | Thickness[ _ ], Red | color specification },-- TourType Vertices | UndirectedEdges | DirectedEdges | Rule | Pairs | None,-- Trace -> True | False,-- SelectedVertexSize -> Automatic | Tiny | Small | Medium | Large | RealNumber,-- SelectedVertexStyle -> Automatic | color specification.
In[]:=
Options[EulerTour]
Out[]=
FontSize10,GraphLayoutAutomatic,ImagePadding15,ImageSizeAutomatic,ItemSizeAutomatic,ManipulateTourTrue,ManipulateWalkFalse,SelectedVertexSizeAutomatic,SelectedVertexStyleAutomatic,SelectionRuleFirst,Spacings{1,Automatic},TableBreaksFalse,TourStyleThickness[Large],
,TourFormatVertices,TraceReduce,VertexCoordinatesAutomatic
Example 34
Example 34
In[]:=
E31={{6,3},{3,11},{6,9},{9,1},{7,11},{3,7},{2,7},{7,12},{12,8},{2,8},{4,11},{11,5},{5,1},{10,3},{4,9}};ME31=ToWeightedAdjacencyMatrix[E31,∞];gE31=ToGraph[E31,∞];
In[]:=
EulerTourQ[ME31,TraceTrue]
Vertices and their degrees: |
{{1,2},{2,2},{3,4},{4,2},{5,2},{6,2},{7,4},{8,2},{9,3},{10,1},{11,4},{12,2}} |
There exists Euler tour between vertices 9 and 10 |
In[]:=
EulerTour[gE31,10,ItemSizeAutomatic,ManipulateWalkTrue,TraceTrue,TableBreaks8,SelectionRuleRandomChoice]
{10,3,7,12,8,2,7,11,4,9,6,3,11,5,1,9}
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
10 | {10,3} | 3 | 1 | {10,3} | 2 |
3 | {3,7} | 7 | 1 | {10,3,7} | 3 |
7 | {7,11} | 11 | 1 | {10,3,7,11} | 4 |
11 | {4,11} | 4 | 1 | {10,3,7,11,4} | 5 |
4 | {4,9} | 9 | 1 | {10,3,7,11,4,9} | 6 |
9 | {6,9} | 6 | 1 | {10,3,7,11,4,9,6} | 7 |
6 | {6,3} | 3 | 1 | {10,3,7,11,4,9,6,3} | 8 |
3 | {3,11} | 11 | 1 | {10,3,7,11,4,9,6,3,11} | 9 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
11 | {11,5} | 5 | 1 | {10,3,7,11,4,9,6,3,11,5} | 10 |
5 | {5,1} | 1 | 1 | {10,3,7,11,4,9,6,3,11,5,1} | 11 |
1 | {9,1} | 9 | 1 | {10,3,7,11,4,9,6,3,11,5,1,9} | 12 |
1 | {9,1} | 9 | -1 | {10,3,7,11,4,9,6,3,11,5,1} ⋃ {1,9} | 11 |
5 | {5,1} | 1 | -1 | {10,3,7,11,4,9,6,3,11,5} ⋃ {5,1,1,9} | 10 |
11 | {11,5} | 5 | -1 | {10,3,7,11,4,9,6,3,11} ⋃ {11,5,5,1,1,9} | 9 |
3 | {3,11} | 11 | -1 | {10,3,7,11,4,9,6,3} ⋃ {3,11,11,5,5,1,1,9} | 8 |
6 | {6,3} | 3 | -1 | {10,3,7,11,4,9,6} ⋃ {6,3,3,11,11,5,5,1,1,9} | 7 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
9 | {6,9} | 6 | -1 | {10,3,7,11,4,9} ⋃ {9,6,6,3,3,11,11,5,5,1,1,9} | 6 |
4 | {4,9} | 9 | -1 | {10,3,7,11,4} ⋃ {4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 5 |
11 | {4,11} | 4 | -1 | {10,3,7,11} ⋃ {11,4,4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 4 |
7 | {7,11} | 11 | -1 | {10,3,7} ⋃ {7,11,11,4,4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 3 |
7 | {7,12} | 12 | 1 | {10,3,7,12} ⋃ {7,11,11,4,4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 4 |
12 | {12,8} | 8 | 1 | {10,3,7,12,8} ⋃ {7,11,11,4,4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 5 |
8 | {2,8} | 2 | 1 | {10,3,7,12,8,2} ⋃ {7,11,11,4,4,9,9,6,6,3,3,11,11,5,5,1,1,9} | 6 |
2 | {2,7} | 7 | 1 | {10,3,7,12,8,2,7,11,4,9,6,3,11,5,1,9} | 16 |
{103,37,712,128,82,27,711,114,49,96,63,311,115,51,19}
{103,37,711,114,49,96,63,311,115,51,19,91,15,511,113,36,69,94,411,117,712,128,82,27,711,114,49,96,63,311,115,51,19}
Example 35
Example 35
In[]:=
E32={{1,11},{1,7},{11,12},{12,1},{1,2},{2,3},{3,4},{4,5},{13,14},{14,4},{5,6},{1,5},{6,4},{2,5},{3,6},{2,7},{5,7},{3,8},{6,8},{9,8},{7,10},{10,8},{11,9},{11,8}};ME32=ToWeightedAdjacencyMatrix[E32,∞];gE32=ToGraph[E32,∞,GraphLayoutAutomatic]
Out[]=
In[]:=
EulerTourQ[E32,TraceTrue]
Vertices and their degrees: |
{{1,5},{2,4},{3,4},{4,4},{5,5},{6,4},{7,4},{8,5},{9,2},{10,2},{11,4},{12,2},{13,1},{14,2}} |
There exists no Euler tour |
In[]:=
EulerTour[gE32,1,ItemSize{2,4,2,2,26,2},ManipulateTourFalse,ManipulateWalkFalse,SelectionRuleLast,TraceFalse,TableBreaks12]
{1,5,7,10,8,11,9,8,6,3,4,6,5,4,14,13}
Example 36
Example 36
In[]:=
E33={{1,2},{2,3},{3,4},{1,5},{5,6},{6,4},{2,5},{3,6},{2,7},{5,7},{3,8},{6,8},{7,9},{9,8},{7,10},{10,8},{5,10},{10,3},{3,11},{11,5},{1,11},{11,4},{4,7},{7,1}};ME33=ToWeightedAdjacencyMatrix[E31,∞];gE33=ToGraph[E33];
In[]:=
EulerTourQ[E33]
There exists closed Euler tour on any vertex |
In[]:=
EulerTour[E33,2,ManipulateWalkTrue,ManipulateTourTrue,SelectionRuleRandomChoice,TraceReduce,VertexCoordinatesEllipse[5,3]]
{2,5,7,4,6,3,4,11,1,5,11,3,2,1,7,9,8,6,5,10,3,8,10,7,2}
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
7 | {2,7} | 2 | 1 | {2,5,7,4,6,3,4,11,1,5,11,3,2,1,7,2} | 16 |
7 | {2,7} | 2 | -1 | {2,5,7,4,6,3,4,11,1,5,11,3,2,1,7} ⋃ {7,2} | 15 |
10 | {7,10} | 7 | 1 | {2,5,7,4,6,3,4,11,1,5,11,3,2,1,7,9,8,6,5,10,7,2} | 22 |
10 | {7,10} | 7 | -1 | {2,5,7,4,6,3,4,11,1,5,11,3,2,1,7,9,8,6,5,10} ⋃ {10,7,7,2} | 20 |
8 | {10,8} | 10 | 1 | {2,5,7,4,6,3,4,11,1,5,11,3,2,1,7,9,8,6,5,10,3,8,10,7,2} | 25 |
{25,57,74,46,63,34,411,111,15,511,113,32,21,17,79,98,86,65,510,103,38,810,107,72}
{25,57,74,46,63,34,411,111,15,511,113,32,21,17,72,27,79,98,86,65,510,107,72,27,710,103,38,810,107,72}
Example 37
Example 37
In[]:=
E34={{1,2},{1,16},{2,3},{2,21},{3,11},{3,13},{4,5},{5,6},{5,7},{5,12},{5,17},{6,7},{7,8},{8,9},{8,20},{9,10},{9,19},{10,5},{10,7},{10,11},{11,4},{11,13},{12,3},{13,2},{13,10},{13,14},{14,10},{15,14},{15,16},{16,13},{16,14},{17,18},{18,8},{19,15},{20,9},{21,1},{21,22},{22,1},{22,15}};
In[]:=
EulerTourQ[E34,TraceTrue]
Vertices and their degrees: |
{{1,4},{2,4},{3,4},{4,2},{5,6},{6,2},{7,4},{8,4},{9,4},{10,6},{11,4},{12,2},{13,6},{14,4},{15,4},{16,4},{17,2},{18,2},{19,2},{20,2},{21,3},{22,3}} |
There exists Euler tour between vertices 21 and 22 |
In[]:=
EulerTour[E34,21,ManipulateWalkTrue,ManipulateTourFalse,SelectionRuleLast,TraceTrue,TableBreaks8]
{21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4,11,3,2,1,22}
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
21 | {21,22} | 22 | 1 | {21,22} | 2 |
22 | {22,15} | 15 | 1 | {21,22,15} | 3 |
15 | {19,15} | 19 | 1 | {21,22,15,19} | 4 |
19 | {9,19} | 9 | 1 | {21,22,15,19,9} | 5 |
9 | {20,9} | 20 | 1 | {21,22,15,19,9,20} | 6 |
20 | {8,20} | 8 | 1 | {21,22,15,19,9,20,8} | 7 |
8 | {18,8} | 18 | 1 | {21,22,15,19,9,20,8,18} | 8 |
18 | {17,18} | 17 | 1 | {21,22,15,19,9,20,8,18,17} | 9 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
17 | {5,17} | 5 | 1 | {21,22,15,19,9,20,8,18,17,5} | 10 |
5 | {10,5} | 10 | 1 | {21,22,15,19,9,20,8,18,17,5,10} | 11 |
10 | {14,10} | 14 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14} | 12 |
14 | {16,14} | 16 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16} | 13 |
16 | {16,13} | 13 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13} | 14 |
13 | {13,14} | 14 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14} | 15 |
14 | {15,14} | 15 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15} | 16 |
15 | {15,16} | 16 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16} | 17 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
16 | {1,16} | 1 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1} | 18 |
1 | {22,1} | 22 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,22} | 19 |
1 | {22,1} | 22 | -1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1} ⋃ {1,22} | 18 |
1 | {21,1} | 21 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21} ⋃ {1,22} | 19 |
21 | {2,21} | 2 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2} ⋃ {1,22} | 20 |
2 | {13,2} | 13 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13} ⋃ {1,22} | 21 |
13 | {13,10} | 10 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10} ⋃ {1,22} | 22 |
10 | {10,11} | 11 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11} ⋃ {1,22} | 23 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
11 | {11,13} | 13 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13} ⋃ {1,22} | 24 |
13 | {3,13} | 3 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3} ⋃ {1,22} | 25 |
3 | {12,3} | 12 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12} ⋃ {1,22} | 26 |
12 | {5,12} | 5 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5} ⋃ {1,22} | 27 |
5 | {5,7} | 7 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7} ⋃ {1,22} | 28 |
7 | {10,7} | 10 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10} ⋃ {1,22} | 29 |
10 | {9,10} | 9 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9} ⋃ {1,22} | 30 |
9 | {8,9} | 8 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8} ⋃ {1,22} | 31 |
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
8 | {7,8} | 7 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7} ⋃ {1,22} | 32 |
7 | {6,7} | 6 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6} ⋃ {1,22} | 33 |
6 | {5,6} | 5 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5} ⋃ {1,22} | 34 |
5 | {4,5} | 4 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4} ⋃ {1,22} | 35 |
4 | {11,4} | 11 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4,11} ⋃ {1,22} | 36 |
11 | {3,11} | 3 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4,11,3} ⋃ {1,22} | 37 |
3 | {2,3} | 2 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4,11,3,2} ⋃ {1,22} | 38 |
2 | {1,2} | 1 | 1 | {21,22,15,19,9,20,8,18,17,5,10,14,16,13,14,15,16,1,21,2,13,10,11,13,3,12,5,7,10,9,8,7,6,5,4,11,3,2,1,22} | 40 |
{2122,2215,1519,199,920,208,818,1817,175,510,1014,1416,1613,1314,1415,1516,161,122,221,121,212,213,1310,1011,1113,133,312,125,57,710,109,98,87,76,65,54,411,113,32,21,122}
Example 38
Example 38
In[]:=
E35={{11,2},{7,6},{5,9},{8,12},{8,2},{9,10},{8,4},{12,5},{7,12},{5,1},{9,1},{3,10},{3,1},{3,8},{10,6},{4,10},{3,7},{8,5},{12,3},{2,6},{7,2},{12,9},{7,8},{1,12},{11,3},{6,8},{11,7}}
Out[]=
{{11,2},{7,6},{5,9},{8,12},{8,2},{9,10},{8,4},{12,5},{7,12},{5,1},{9,1},{3,10},{3,1},{3,8},{10,6},{4,10},{3,7},{8,5},{12,3},{2,6},{7,2},{12,9},{7,8},{1,12},{11,3},{6,8},{11,7}}
In[]:=
EulerTourQ[E35]
There exists Euler tour between vertices 8 and 11 |
In[]:=
EulerTour[E35,8,ManipulateWalkTrue,ManipulateTourFalse,SelectionRuleFirst,TraceReduce,VertexCoordinatesEllipse[6,3]]
{8,12,5,9,10,3,1,5,8,2,11,3,8,4,10,6,7,12,9,1,12,3,7,2,6,8,7,11}
v 1 | e | v 2 | c(e) | Σ ⋃ Ω | Σ |
7 | {11,7} | 11 | 1 | {8,12,5,9,10,3,1,5,8,2,11,3,8,4,10,6,7,12,3,7,2,6,8,7,11} | 25 |
12 | {12,3} | 3 | -1 | {8,12,5,9,10,3,1,5,8,2,11,3,8,4,10,6,7,12} ⋃ {12,3,3,7,7,2,2,6,6,8,8,7,7,11} | 18 |
1 | {1,12} | 12 | 1 | {8,12,5,9,10,3,1,5,8,2,11,3,8,4,10,6,7,12,9,1,12,3,7,2,6,8,7,11} | 28 |
{812,125,59,910,103,31,15,58,82,211,113,38,84,410,106,67,712,123,37,72,26,68,87,711,117,78,86,62,27,73,312,129,91,112,123,37,72,26,68,87,711}
Pairing in Bipartite (Dissected) Graphs
Pairing in Bipartite (Dissected) Graphs
AugmentingPath
AugmentingPath
Alternatives for optional arguments of AugmentigPath:-- AugmentedPairing ->True | False,-- AugmentingPath -> True | False,-- FromWhere -> True | False,-- FontSize -> 10 | Any font size,-- ImageSize -> Automatic | {450, 150} | Any dimensions,-- ImagePadding -> 10 | Any size,-- ItemSize -> Automatic | List, -- Manipulate -> True | False,-- SelectionRule First | Last | RandomChoice,-- Sort -> True | False,-- Spacings -> Automatic | {0.5, 0,8} | as for Grid-- Trace -> True | Reduce | False,-- VertexCoordinates Automatic | “XY” | Orthogon[positive number].
In[]:=
Options[AugmentingPath]
Out[]=
{AugmentedPairingTrue,AugmentingPathTrue,FontSize10,FromWhereTrue,ImageSizeAutomatic,ImagePadding10,ItemSizeAutomatic,ManipulateTrue,SelectionRuleFirst,SortFalse,Spacings{1,0.75},TraceTrue,VertexCoordinatesAutomatic}
MaxPairing
MaxPairing
Alternatives for optional arguments of MaxPairing:-- FontSize -> 10 | any font size,-- ItemSize -> Automatic | List, -- Print => All | Last,-- SelectionRule First | Last | RandomChoice,-- Sort -> True | False,-- Spacings -> Automatic | {0.5, 0,8} | {number | Automatic, number | Automatic} | number,-- Trace -> True | Reduce | False.
In[]:=
Options[MaxPairing]
Out[]=
{AugmentedPairingTrue,AugmentingPathTrue,FontSize10,FromWhereTrue,ItemSizeAutomatic,PrintAll,SelectionRuleFirst,SortFalse,Spacings{1,0.75},TraceTrue}
Example 39: X = Range[1, 7], Y = Range[8, 15]
Example 39: X = Range[1, 7], Y = Range[8, 15]
In[]:=
X36=Range[1,7];Y36=Range[8,15];E36={{1,10},{1,11},{2,8},{2,9},{3,8},{3,9},{3,11},{4,12},{4,13},{4,14},{4,15},{5,9},{5,10},{5,12},{6,8},{6,13},{7,11},{7,14}};P36={110,28,39,412,711};gE36=ToGraph[E36,∞];ToGraph[E36,∞,EdgeStyleMap[#{Thick,Green}&,P36],GraphLayoutAutomatic,VertexCoordinatesOrthogon[7,8,1],VertexLabels->OrthogonLabels[7,8],ImagePadding10,ImageSize{450,150}]
Out[]=
In[]:=
{DissectedGraphQ[gE36,X36,Y36],MaxPairingLength[gE36,X36]}
Out[]=
{True,7}
In[]:=
P36={110,28,39,412,711};AugmentingPath[gE36,X36,Y36,P36,SelectionRuleFirst,VertexCoordinatesOrthogon[1],TraceTrue]
Initial pairing: |
{110,28,39,412,711} |
Extension of initial pairing found in a trivial way: |
{110,28,39,412,711,613} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{5} | |||||||
∅ | 5 | 59 | 9 | 39 | 3 | {3} | {59,93} |
∅ | 5 | 510 | 10 | 110 | 1 | {3,1} | {510,101} |
∅ | 5 | 512 | 12 | 412 | 4 | {3,1,4} | {512,124} |
{3,1,4} | |||||||
{1,4} | 3 | 38 | 8 | 28 | 2 | {2} | {38,82} |
{1,4} | 3 | 311 | 11 | 711 | 7 | {2,7} | {311,117} |
∅ | 4 | 413 | 13 | 613 | 6 | {2,7,6} | {413,136} |
∅ | 4 | 414 | 14 | {2,7,6} | {414} | ||
∅ |
FromWhereList: |
{59,39,510,110,512,412,38,28,311,711,413,613,414} |
Augmenting Path: |
{512,412,414} |
Augmented Pairing: |
{110,28,39,711,613,512,414} |
In[]:=
AugmentingPath[E36,X36,Y36,SelectionRuleFirst,VertexCoordinatesOrthogon[1],TraceReduce]
A pairing found in a trivial way: |
{110,28,39,412,613,711} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
∅ | 5 | 59 | 9 | 39 | 3 | {3} | {59,93} |
∅ | 5 | 510 | 10 | 110 | 1 | {3,1} | {510,101} |
∅ | 5 | 512 | 12 | 412 | 4 | {3,1,4} | {512,124} |
{1,4} | 3 | 38 | 8 | 28 | 2 | {2} | {38,82} |
{1,4} | 3 | 311 | 11 | 711 | 7 | {2,7} | {311,117} |
∅ | 4 | 413 | 13 | 613 | 6 | {2,7,6} | {413,136} |
∅ | 4 | 414 | 14 | {2,7,6} | {414} |
FromWhereList: |
{59,39,510,110,512,412,38,28,311,711,413,613,414} |
Augmenting Path: |
{512,412,414} |
Augmented Pairing: |
{110,28,39,613,711,512,414} |
| |||||||
|
In[]:=
MaxPairing[E36,X36,Y36,P36,AugmentingPathTrue,AugmentedPairingTrue,SelectionRuleLast,FromWhereTrue,TraceTrue]
Initial pairing: |
{110,28,39,412,711} |
Extension of initial pairing found in a trivial way: |
{110,28,39,412,711,613} |
|
FromWhereList: |
{512,124,510,101,59,93,311,117,38,82,415} |
Augmenting Path: |
{512,412,415} |
Augmented Pairing: |
{110,28,39,711,613,512,415} |
*** |
The last pairing is maximal. |
Example 40: X = Range[1, 7], Y = Range[8, 15]
Example 40: X = Range[1, 7], Y = Range[8, 15]
In[]:=
X37=Range[1,7];Y37=Range[8,15];E37={{1,11},{1,12},{1,14},{1,15},{2,8},{2,11},{2,15},{3,9},{3,11},{3,12},{3,14},{4,12},{4,13},{4,14},{5,9},{5,10},{5,11},{5,12},{6,10},{6,11},{6,12},{6,9},{7,8},{7,9},{7,10},{7,11}};E37={{1,11},{1,12},{1,14},{1,15},{2,8},{2,11},{2,15},{3,9},{3,11},{3,12},{3,14},{4,12},{4,13},{4,14},{5,10},{5,11},{5,12},{6,10},{6,11},{6,12},{7,10},{7,11}};P37={111,28,39,412,510};gE37=ToGraph[E37,∞];ToGraph[E37,∞,EdgeStyleMap[#{Thick,Green}&,P37],GraphLayoutAutomatic,VertexCoordinatesOrthogon[7,8,1],VertexLabels->OrthogonLabels[7,8],ImagePadding10]
Out[]=
In[]:=
{DissectedGraphQ[gE37,X37,Y37],MaxPairingLength[gE37,X37]}
Out[]=
{True,7}
In[]:=
P37={111,28,39,412,510};AugmentingPath[E37,X37,Y37,P37,SelectionRuleLast,ManipulateTrue,TraceTrue,VertexCoordinates"XY"]
Initial pairing: |
{111,28,39,412,510} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{6,7} | |||||||
{6} | 7 | 711 | 11 | 111 | 1 | {1} | {711,111} |
{6} | 7 | 710 | 10 | 510 | 5 | {1,5} | {710,105} |
∅ | 6 | 612 | 12 | 412 | 4 | {1,5,4} | {612,124} |
{1,5,4} | |||||||
{1,5} | 4 | 414 | 14 | ∅ | {414} | ||
∅ | 1 | 115 | 15 | ∅ | {115} | ||
∅ |
FromWhereList: |
{711,111,710,510,612,412,414,115} |
Augmenting Path: |
{711,111,115} |
Augmented Pairing: |
{28,39,412,510,711,115} |
In[]:=
AugmentingPath[E37,X37,Y37,SelectionRuleLast,ManipulateTrue,TraceTrue,VertexCoordinates"XY"]
A pairing found in a trivial way: |
{711,612,510,414,39,215} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{1} | |||||||
∅ | 1 | 115 | 15 | 215 | 2 | {2} | {115,152} |
∅ | 1 | 114 | 14 | 414 | 4 | {2,4} | {114,144} |
∅ | 1 | 112 | 12 | 612 | 6 | {2,4,6} | {112,126} |
∅ | 1 | 111 | 11 | 711 | 7 | {2,4,6,7} | {111,117} |
{2,4,6,7} | |||||||
{2,4,6} | 7 | 710 | 10 | 510 | 5 | {5} | {710,105} |
{2} | 4 | 413 | 13 | {5} | {413} | ||
∅ | 2 | 28 | 8 | ∅ | {28} | ||
∅ |
FromWhereList: |
{115,215,114,414,112,612,111,711,710,510,413,28} |
Augmenting Path: |
{115,215,28} |
Augmented Pairing: |
{711,612,510,414,39,115,28} |
In[]:=
MaxPairing[E37,X37,Y37,FromWhereTrue,TraceTrue,PrintAll]
Extension of initial pairing found in a trivial way: |
{111,28,39,412,510} |
|
FromWhereList: |
{610,105,611,111,612,124,114,413} |
Augmenting Path: |
{612,412,413} |
Augmented Pairing: |
{111,28,39,510,612,413} |
*** |
|
FromWhereList: |
{710,105,711,111,512,126,114} |
Augmenting Path: |
{711,111,114} |
Augmented Pairing: |
{28,39,510,612,413,711,114} |
*** |
The last pairing is maximal. |
In[]:=
MaxPairingLength[gE37,X37]
Out[]=
7
Example 41: X = Range[1, 7], Y = Range[8, 14]
Example 41: X = Range[1, 7], Y = Range[8, 14]
In[]:=
X38=Range[1,7];Y38=Range[8,14];E38={{1,11},(*{1,12},{1,14},{1,15},*){2,8},(*{2,11},{2,15},*){3,9},{3,11},{3,12},{3,14},{4,12},{4,13},{4,14},{5,9},{5,10},{5,11},(*{5,12},{6,10},{6,11},*){6,12},{6,9},{7,8},{7,9},{7,10},{7,11}};ME38=ToWeightedAdjacencyMatrix[E38,∞];P38={111,28,39,412,510};gE38=ToGraph[E38,∞];ToGraph[E38,∞,EdgeStyleMap[#{Thick,Green}&,P38],GraphLayoutAutomatic,VertexCoordinatesOrthogon[7,7,1.5],VertexLabelsOrthogonLabels[7,7],ImagePadding10]
Out[]=
In[]:=
{DissectedGraphQ[gE38,X38],MaxPairingLength[gE38,X38]}
Out[]=
{True,7}
In[]:=
P38={111,28,39,412,510};AugmentingPath[E38,X38,Y38,P38,SelectionRuleLast,VertexCoordinatesOrthogon[1]]
Initial pairing: |
{111,28,39,412,510} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{6,7} | |||||||
{6} | 7 | 711 | 11 | 111 | 1 | {1} | {711,111} |
{6} | 7 | 710 | 10 | 510 | 5 | {1,5} | {710,105} |
{6} | 7 | 79 | 9 | 39 | 3 | {1,5,3} | {79,93} |
{6} | 7 | 78 | 8 | 28 | 2 | {1,5,3,2} | {78,82} |
∅ | 6 | 612 | 12 | 412 | 4 | {1,5,3,2,4} | {612,124} |
{1,5,3,2,4} | |||||||
{1,5,3,2} | 4 | 414 | 14 | ∅ | {414} | ||
∅ |
FromWhereList: |
{711,111,710,510,79,39,78,28,612,412,414} |
Augmenting Path: |
{612,412,414} |
Augmented Pairing: |
{111,28,39,510,612,414} |
In[]:=
MaxPairing[ME38,X38,Y38,P38]
Initial pairing: |
{111,28,39,412,510} |
|
FromWhereList: |
{69,93,612,124,78,82,710,105,711,111,314,413} |
Augmenting Path: |
{612,412,413} |
Augmented Pairing: |
{111,28,39,510,612,413} |
*** |
|
FromWhereList: |
{78,82,79,93,710,105,711,111,312,126,314} |
Augmenting Path: |
{79,39,314} |
Augmented Pairing: |
{111,28,510,612,413,79,314} |
*** |
The last pairing is maximal. |
In[]:=
MaxPairingLength[ME38,X38]
Out[]=
7
Example 42: X = Range[1, 8], Y = Range[9, 16]
Example 42: X = Range[1, 8], Y = Range[9, 16]
In[]:=
X39=Range[1,8];Y39=Range[9,16];E39={{1,9},{1,10},{1,12},{1,13},{2,10},{2,12},{3,9},{3,11},{3,13},{4,9},{4,12},{4,14},{4,15},{4,16},{5,13},{5,14},{5,15},{5,16},{6,9},{6,12},{7,10},{7,11},{8,10},{8,11}};P39={19,210,311,412,513};gE39=ToGraph[E39,∞];ToGraph[E39,∞,EdgeStyleMap[#{Thick,Green}&,P39],GraphLayoutAutomatic,VertexCoordinatesOrthogon[8,8,1],VertexLabelsOrthogonLabels[8,8],ImagePadding10]
Out[]=
In[]:=
{DissectedGraphQ[E39,X39,Y39],MaxPairingLength[E39,X39]}
Out[]=
{False,7}
In[]:=
AugmentingPath[gE39,X39,Y39,P39,SelectionRuleRandomChoice,VertexCoordinates"XY"]
Initial pairing: |
{19,210,311,412,513} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{6,7,8} | |||||||
{6,7} | 8 | 810 | 10 | 210 | 2 | {2} | {810,102} |
{6,7} | 8 | 811 | 11 | 311 | 3 | {2,3} | {811,113} |
∅ | 6 | 69 | 9 | 19 | 1 | {2,3,1} | {69,91} |
∅ | 6 | 612 | 12 | 412 | 4 | {2,3,1,4} | {612,124} |
{2,3,1,4} | |||||||
{3,4} | 1 | 113 | 13 | 513 | 5 | {5} | {113,135} |
{3} | 4 | 414 | 14 | {5} | {414} | ||
∅ |
FromWhereList: |
{810,210,811,311,69,19,612,412,113,513,414} |
Augmenting Path: |
{612,412,414} |
Augmented Pairing: |
{19,210,311,513,612,414} |
In[]:=
P39={19,210,311,412,513};MaxPairing[E39,X39,Y39,P39,SelectionRuleRandomChoice,TraceReduce];
Initial pairing: |
{19,210,311,412,513} |
|
FromWhereList: |
{811,113,810,102,612,124,69,91,313,135,415} |
Augmenting Path: |
{612,412,415} |
Augmented Pairing: |
{19,210,311,513,612,415} |
*** |
|
FromWhereList: |
{810,102,811,113,39,91,313,135,212,126,514} |
Augmenting Path: |
{811,311,313,513,514} |
Augmented Pairing: |
{19,210,612,415,811,313,514} |
*** |
|
FromWhereList: |
{711,118,710,102,212,126,69,91,113,133} |
There is no augmenting Path. |
*** |
The last pairing is maximal |
Example 43: X = Range[1, 7], Y = Range[8, 15]
Example 43: X = Range[1, 7], Y = Range[8, 15]
In[]:=
X40=Range[1,7];Y40=Range[8,15];E40={{1,11},{2,9},{2,10},{3,8},{3,13},{4,9},{4,10},{4,14},{5,11},{5,12},{6,12},{6,14},{6,15},{7,10},{7,12}};P40={29,38,511,615,712};gE40=ToGraph[E40,∞];ToGraph[E40,∞,EdgeStyleMap[#{Thick,Green}&,P40],GraphLayoutAutomatic,VertexCoordinatesOrthogon[7,8,1],VertexLabelsOrthogonLabels[7,8],ImagePadding10]
Out[]=
In[]:=
{DissectedGraphQ[gE40,X40],MaxPairingLength[E40,X40]}
Out[]=
{True,7}
In[]:=
AugmentingPath[gE40,X40,Y40,P40,SelectionRuleRandomChoice,VertexCoordinates"XY"]
Initial pairing: |
{29,38,511,615,712} |
Extension of initial pairing found in a trivial way: |
{29,38,511,615,712,410} |
Mx | x | xy | y | zy | z | Mz | FromWhere |
{1} | |||||||
∅ | 1 | 111 | 11 | 511 | 5 | {5} | {111,115} |
{5} | |||||||
∅ | 5 | 512 | 12 | 712 | 7 | {7} | {512,127} |
{7} | |||||||
∅ | 7 | 710 | 10 | 410 | 4 | {4} | {710,104} |
{4} | |||||||
∅ | 4 | 49 | 9 | 29 | 2 | {2} | {49,92} |
∅ | 4 | 414 | 14 | {2} | {414} | ||
∅ |
FromWhereList: |
{111,511,512,712,710,410,49,29,414} |
Augmenting Path: |
{111,511,512,712,710,410,414} |
Augmented Pairing: |
{29,38,615,111,512,710,414} |
In[]:=
P40={29,38,511,712};MaxPairing[gE40,X40,Y40,P40,SelectionRuleRandomChoice,TraceReduce];
Initial pairing: |
{29,38,511,712} |
Extension of initial pairing found in a trivial way: |
{29,38,511,712,410,614} |
|
FromWhereList: |
{111,115,512,127,710,104,414,146,49,92,615} |
Augmenting Path: |
{111,511,512,712,710,410,414,614,615} |
Augmented Pairing: |
{29,38,111,512,710,414,615} |
*** |
The last pairing is maximal. |
Flows in Networks
Flows in Networks
Incrementing Path Algorithm
Incrementing Path Algorithm
Alternatives for Incrementing Path Algorithm:-- Flow -> True | Essentialpart |False,-- FontSize -> 10 | any font size,-- ItemSize -> Automatic | List of numbers, -- Metod -> MaximalCapacity | ShortestPath,-- Print -> All | Last,-- SelectionRule First | Last | RandomChoice,-- Spacings -> Automatic | {0.5, 0,8} | {number | Automatic, number | Automatic} | number,-- Trace -> True | Reduced | False.
In[]:=
Options[IncrementingPath]
Out[]=
{FlowTrue,FontSize10,FrameAll,ItemSizeAutomatic,MethodMaximalCapacity,PathTrue,SelectionRuleFirst,Spacings{1,0.5},TraceTrue}
Ford-Fulkerson Algorithm
Ford-Fulkerson Algorithm
Alternatives for optional arguments of FordFulkerson:-- Flow -> True | EssentialPart,-- FontSize -> 10 | any font size,-- ItemSize -> Automatic | List of numbers, -- Metod -> MaximalCapacity | ShortestPath,-- Print -> All | Last,-- SelectionRule First | Last | RandomChoice,-- Spacings -> Automatic | {0.5, 0,8} | {number | Automatic, number | Automatic} | number,-- Trace -> True | Reduced | False.
In[]:=
Options[FordFulkerson]
Out[]=
{FlowTrue,FrameAll,FontSize10,ItemSizeAutomatic,MethodMaximalCapacity,PathTrue,SelectionRuleFirst,PrintAll,SpacingsAutomatic,TraceTrue}
Example 44
Example 44
In[]:=
E41={{1,2},{1,3},{1,6},{2,3},{2,4},{3,4},{3,5},{4,5},{4,8},{5,1},{5,6},{5,7},{5,8},{6,9},{7,4},{7,9},{8,9}};ME41=ToWeightedAdjacencyMatrix[E41];gE41=ToGraph[E41];ToGraph[E41,AspectRatio1/3,GraphLayoutAutomatic,ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
cfl41={{6,0,0},{6,2,0},{3,3,0},{2,0,0},{3,0,0},{4,0,0},{4,2,0},{5,4,3},{3,0,0},{3,1,1},{6,1,0},{5,4,0},{5,0,0},{5,4,0},{6,4,2},{3,0,0},{9,0,0}};c41=#[[1]]&/@cfl41;f41=#[[2]]&/@cfl41;l41=#[[3]]&/@cfl41;Ecfl41=MapThread[Join,{E41,cfl41}];Grid[Prepend[Ecfl41,{"Tail","Head","c","f","l"}]//Transpose,DividersAll,Spacings{1,0.5}]
Out[]=
Tail | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 4 | 5 | 5 | 5 | 5 | 6 | 7 | 7 | 8 |
Head | 2 | 3 | 6 | 3 | 4 | 4 | 5 | 5 | 8 | 1 | 6 | 7 | 8 | 9 | 4 | 9 | 9 |
c | 6 | 6 | 3 | 2 | 3 | 4 | 4 | 5 | 3 | 3 | 6 | 5 | 5 | 5 | 6 | 3 | 9 |
f | 0 | 2 | 3 | 0 | 0 | 0 | 2 | 4 | 0 | 1 | 1 | 4 | 0 | 4 | 4 | 0 | 0 |
l | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 0 | 1 | 0 | 0 | 0 | 0 | 2 | 0 | 0 |
In[]:=
{NetworkQ[gE41,c41,l41],FlowQ[E41,{1,9},f41],FlowValue[E41,{1,9},f41]}
Out[]=
{True,True,4}
In[]:=
In[]:=
IncrementingPath[gE41,c41,l41,{1,9},f41,FontSize10,MethodMaximalCapacity,SelectionRuleFirst,TraceTrue]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
IncrementingPath[E41,c41,l41,{1,9},f41,FlowTrue,FontSize10,MethodShortestPath,SelectionRuleLast,TraceTrue]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
FordFulkerson[gE41,c41,l41,{1,9},f41,PrintLast,MethodMaximalCapacity,PathFalse,SelectionRuleFirst,TraceReduce]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path. | ||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||
The last flow with value 12 is maximal | ||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
FordFulkerson[gE41,c41,l41,{1,9},f41,PrintAll,MethodMaximalCapacity,SelectionRuleRandomChoice,TraceFalse]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path |
The last flow with value 12 is maximal. |
★★★ |
In[]:=
FordFulkerson[gE41,c41,l41,{1,9},f41,PrintLast,MethodShortestPath,SelectionRuleFirst,TraceFalse]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path. |
The last flow with value 12 is maximal |
★★★ |
In[]:=
FordFulkerson[gE41,c41,l41,{1,9},f41,PrintAll,MethodMaximalCapacity,SelectionRuleFirst,TraceFalse]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path |
The last flow with value 12 is maximal. |
★★★ |
In[]:=
FordFulkerson[gE41,c41,l41,{1,9},f41,PrintAll,MethodShortestPath,SelectionRuleFirst,TraceFalse]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path |
The last flow with value 12 is maximal. |
★★★ |
Example 45
Example 45
In[]:=
E42={{1,2},{1,3},{1,6},{2,3},{2,4},{3,4},{3,5},{4,5},{8,4},{5,1},{5,6},{5,7},{5,8},{6,8},{7,4},{9,7},{9,8}};gE42=Graph[E42,DirectedEdgesTrue,ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
cfl42={{6,0,0},{6,0,0},{4,0,0},{2,0,0},{3,0,0},{4,0,0},{4,0,0},{5,4,3},{3,0,0},{3,0,0},{6,0,0},{5,4,0},{5,0,0},{5,0,0},{6,4,2},{3,0,0},{9,0,0}};c42=#[[1]]&/@cfl42;f42=#[[2]]&/@cfl42;l42=#[[3]]&/@cfl42;Ecfl42=MapThread[Join,{E42,cfl42}];Grid[Prepend[Ecfl42,{"Tail","Head","c","f","l"}]//Transpose,DividersAll]
Out[]=
Tail | 1 | 1 | 1 | 2 | 2 | 3 | 3 | 4 | 8 | 5 | 5 | 5 | 5 | 6 | 7 | 9 | 9 |
Head | 2 | 3 | 6 | 3 | 4 | 4 | 5 | 5 | 4 | 1 | 6 | 7 | 8 | 8 | 4 | 7 | 8 |
c | 6 | 6 | 4 | 2 | 3 | 4 | 4 | 5 | 3 | 3 | 6 | 5 | 5 | 5 | 6 | 3 | 9 |
f | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 0 | 0 | 0 | 4 | 0 | 0 | 4 | 0 | 0 |
l | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 0 | 0 | 0 | 0 | 0 | 0 | 2 | 0 | 0 |
In[]:=
{NetworkQ[gE42,c42,l42],FlowQ[E42,{1,9},f42],FlowValue[E42,{1,9},f42]}
Out[]=
{True,True,0}
In[]:=
IncrementingPath[gE42,c42,l42,{1,9},f42,FlowTrue,FontSize10,MethodMaximalCapacity,PathTrue,SelectionRuleFirst,TraceTrue]
There is no f-incrementing path | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
FordFulkerson[gE42,c42,l42,{1,9},f42,PrintLast,TraceReduce,MethodShortestPath]
| ||
★★★ |
There is no f-incrementing path. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The last flow with value GraphAlgorithms`Private`value$13506 is maximal | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
Example 46
Example 46
In[]:=
E43={{1,7},{1,11},{2,3},{2,8},{2,11},{3,4},{3,8},{3,13},{4,5},{4,10},{4,13},{4,14},{5,6},{5,10},{7,2},{7,3},{7,8},{8,4},{8,9},{9,4},{9,5},{9,10},{10,6},{11,3},{11,12},{12,3},{12,4},{12,13},{13,14},{14,5},{14,6}};gE43=Graph[E43,DirectedEdgesTrue,GraphLayout"SpringEmbedding",ImagePadding10,VertexLabels"Name"]
Out[]=
In[]:=
c43={2,1,1,2,1,1,1,2,1,2,1,2,2,1,1,1,2,1,1,1,1,1,2,2,1,1,2,1,1,1,2};f43=l43=0*Range[Length@E43];cfl43=Thread[List[c43,f43,l43]];Ecfl43=MapThread[Join,{E43,cfl43}];StylePrint/@{Grid[Transpose[Join[{{"Tail","Head","c","f","l"}},Ecfl43[[;;16]]]],FrameAll],Grid[Transpose[Join[{{"Tail","Head","c","f","l"}},Ecfl43[[17;;]]]],FrameAll]};
Tail | 1 | 1 | 2 | 2 | 2 | 3 | 3 | 3 | 4 | 4 | 4 | 4 | 5 | 5 | 7 | 7 |
Head | 7 | 11 | 3 | 8 | 11 | 4 | 8 | 13 | 5 | 10 | 13 | 14 | 6 | 10 | 2 | 3 |
c | 2 | 1 | 1 | 2 | 1 | 1 | 1 | 2 | 1 | 2 | 1 | 2 | 2 | 1 | 1 | 1 |
f | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
l | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Tail | 7 | 8 | 8 | 9 | 9 | 9 | 10 | 11 | 11 | 12 | 12 | 12 | 13 | 14 | 14 |
Head | 8 | 4 | 9 | 4 | 5 | 10 | 6 | 3 | 12 | 3 | 4 | 13 | 14 | 5 | 6 |
c | 2 | 1 | 1 | 1 | 1 | 1 | 2 | 2 | 1 | 1 | 2 | 1 | 1 | 1 | 2 |
f | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
l | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
In[]:=
{NetworkQ[gE43,c43,l43],FlowQ[E43,{1,9},f43],FlowValue[E43,{1,9},f43]}
Out[]=
{True,True,0}
In[]:=
IncrementingPath[gE43,c43,l43,{1,6},f43,FlowEssentialPart,FontSize10,PathTrue,MethodShortestPath,PathTrue,SelectionRuleRandomChoice,TraceReduce]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
IncrementingPath[gE43,c43,l43,{1,6},f43,FlowEssentialPart,FontSize10,PathTrue,MethodMaximalCapacity,SelectionRuleRandomChoice,TraceTrue]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
In[]:=
FordFulkerson[gE43,c43,{1,6},f43,FlowEssentialPart,MethodMaximalCapacity,PrintAll,SelectionRuleRandomChoice,TraceReduce]
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
★★★ |
There is no f-incrementing path | ||||||||||||||||||||||||
| ||||||||||||||||||||||||
The last flow with value 3 is maximal. | ||||||||||||||||||||||||
★★★ |
Definitions


Cite this as: Vojtěch (Vojtech) Bartík (Bartik), "Graph algorithms" from the Notebook Archive (2021), https://notebookarchive.org/2021-11-a6bkcrc

Download

