Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- adjMat1 = {{Infinity, 1, 1, 1, 1},
- {1, Infinity, 1, 1, 1},
- {1, 1, Infinity, 1, 1},
- {1, 1, 1, Infinity, 1},
- {1, 1, 1, 1, Infinity}};
- adjMat2 = {{Infinity, 8, 6, 3, 7},
- {1, Infinity, 1, 1, 1},
- {2, 5, Infinity, 9, 4},
- {1, 1, 1, Infinity, 1},
- {1, 1, 1, 1, Infinity}};
- WeightedAdjacencyGraph[adjMat1, DirectedEdges -> True]
- WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True]
- adjMat1 = {{Infinity, 1, 1, 1, 1}, {1, Infinity, 1, 1, 1}, {1, 1,
- Infinity, 1, 1}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
- adjMat2 = {{Infinity, 8, 6, 3, 7}, {1, Infinity, 1, 1, 1}, {2, 5,
- Infinity, 9, 4}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
- multiedge = DeleteCases[ Flatten@Table[
- If[i == j, Null, Table[i -> j, {adjMat2[[i, j]]}]], {i,
- Length[adjMat2]}, {j, Length@First@adjMat2}], Null]
- (* Output: {1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2,
- 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 4, 1 -> 4,
- 1 -> 4, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5,
- 2 -> 1, 2 -> 3, 2 -> 4, 2 -> 5, 3 -> 1, 3 -> 1, 3 -> 2, 3 -> 2,
- 3 -> 2, 3 -> 2, 3 -> 2, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4,
- 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 5, 3 -> 5, 3 -> 5, 3 -> 5,
- 4 -> 1, 4 -> 2, 4 -> 3, 4 -> 5, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4} *)
- Manipulate[
- Row[{GraphPlot[multiedge, MultiedgeStyle -> None, Method -> s],
- GraphPlot[adjMat1, MultiedgeStyle -> None, Method -> s],
- AdjacencyGraph[adjMat1 /. [Infinity] -> 0,
- GraphLayout -> s]}], {s, {"SpringElectricalEmbedding",
- "SpringEmbedding", "HighDimensionalEmbedding", "CircularEmbedding",
- "RandomEmbedding", "LinearEmbedding"}}]
- Options[VisualizeNetwork] = Options[Graph];
- VisualizeNetwork[{adjMat2_, edgecoloring_: {Green, Red},
- scaling_: 600, opacity_: 0.5, ShowEdgeWeight_: True},
- opts : OptionsPattern[]] :=
- Block[{gr, weight, Nweight, edge, nodes, vertexStyle, EdgeBlendColor,edgeStyle, g},
- gr = WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True];
- (*Lets extract the edge weight list from your graph object*)
- weight = AbsoluteOptions[gr, EdgeWeight][[1, 2]];
- (*Extract the edge list*)
- edge = EdgeList@gr;
- (*Extract the vertex list*)
- nodes = VertexList@gr;
- (* Make a color blend by scaling all the edge weight with the maximum edge weight *)
- EdgeBlendColor = Map[ Blend[edgecoloring, #/Max[weight]] &, weight];
- (* Make a thickness by scaling all the edge weight with the maximum edge weight *)
- edgeStyle =
- MapThread[(#1 -> {Opacity[opacity], #3,
- Thickness[#2/scaling]}) &, {edge, weight, EdgeBlendColor}];
- Nweight = MapThread[
- Rasterize[#1, ImageSize -> 9,
- Background -> Lighter@#2] &, {weight, EdgeBlendColor}];
- g = If[ShowEdgeWeight == True,
- Graph[nodes, edge, EdgeStyle -> edgeStyle,
- EdgeLabels -> Flatten@MapThread[#1 -> #2 &, {edge, Nweight}],
- VertexLabels ->
- Table[i -> Placed["Name", {1/2, 1/2}], {i, nodes}], opts],
- Graph[nodes, edge, EdgeStyle -> edgeStyle,
- VertexLabels ->
- Table[i -> Placed["Name", {1/2, 1/2}], {i, nodes}], opts]
- ];
- g
- ];
- adjMat1 = {{Infinity, 1, 1, 1, 1}, {1, Infinity, 1, 1, 1}, {1, 1,
- Infinity, 1, 1}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
- adjMat2 = {{Infinity, 8, 6, 3, 7}, {1, Infinity, 1, 1, 1},
- {2, 5,Infinity, 9, 4}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
- (* Some Graph visualization option *)
- opts = {VertexSize -> Medium, VertexSize -> Medium,
- VertexStyle ->Directive[Opacity[0.65`], Blue, EdgeForm[None]],
- VertexLabelStyle -> Directive[GrayLevel[0], 12],
- VertexSize -> {"Scaled", 0.3`},
- GraphLayout -> "SpringElectricalEmbedding"};
- (* Call the function *)
- GraphicsGrid@{{VisualizeNetwork[{adjMat1, {Yellow, Red}, 600, 0.5,
- True}, opts],
- VisualizeNetwork[{adjMat2, {Yellow, Red}, 600, 0.5, True}, opts]}}
- gr = WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True];
- weight = AbsoluteOptions[gr, EdgeWeight][[1, 2]];
- edge = EdgeList@gr;
- k = Graph[edge, EdgeLabels -> (el=MapThread[#1 -> #2 &, {edge, weight}])];
- g = (AbsoluteOptions[k,
- EdgeLabels] /. {(a_ [DirectedEdge] b_ -> c_) :> {a -> b,
- c}})[[1, 2]];
- GraphPlot[g,
- EdgeRenderingFunction -> ({Text[Style[#3, 15], Mean[#1]], Blue,
- AbsoluteThickness[0.5 + #3/5], Arrowheads[0.02 + #3/170],
- Arrow[#1, 0.075]} &), VertexLabeling -> True]
- {e, w} = Transpose[g]
- Graph[e, EdgeWeight -> w,
- EdgeLabelStyle -> Directive[Red, 20, Background -> White],
- EdgeLabels -> el ,
- EdgeShapeFunction ->
- el /. {((a_ [DirectedEdge] b_) -> c_) -> ((a [DirectedEdge]
- b) -> ({AbsoluteThickness[.9 + c/4], Arrow[#1, 0.025]} &))}]
- Graph[{1 <-> 2, 2 <-> 3}, EdgeWeight -> {1, 2},
- GraphLayout -> {"SpringElectricalEmbedding", "EdgeWeighted" -> True}]
- <<IGraphM`
- g = Graph[{1 <-> 2, 2 <-> 3}, EdgeWeight -> {1, 2}];
- IGLayoutFruchtermanReingold[g]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement