SHARE
TWEET

Untitled

a guest Apr 21st, 2017 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. adjMat1 = {{Infinity, 1, 1, 1, 1},
  2. {1, Infinity, 1, 1, 1},
  3. {1, 1, Infinity, 1, 1},
  4. {1, 1, 1, Infinity, 1},
  5. {1, 1, 1, 1, Infinity}};
  6.  
  7. adjMat2 = {{Infinity, 8, 6, 3, 7},
  8. {1, Infinity, 1, 1, 1},
  9. {2, 5, Infinity, 9, 4},
  10. {1, 1, 1, Infinity, 1},
  11. {1, 1, 1, 1, Infinity}};
  12.  
  13. WeightedAdjacencyGraph[adjMat1, DirectedEdges -> True]
  14. WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True]
  15.    
  16. adjMat1 = {{Infinity, 1, 1, 1, 1}, {1, Infinity, 1, 1, 1}, {1, 1,
  17.     Infinity, 1, 1}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
  18.    
  19. adjMat2 = {{Infinity, 8, 6, 3, 7}, {1, Infinity, 1, 1, 1}, {2, 5,
  20.     Infinity, 9, 4}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
  21.    
  22. multiedge = DeleteCases[ Flatten@Table[
  23.     If[i == j, Null, Table[i -> j, {adjMat2[[i, j]]}]], {i,
  24.      Length[adjMat2]}, {j, Length@First@adjMat2}], Null]
  25.  
  26. (* Output:   {1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2, 1 -> 2,
  27.  1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 3, 1 -> 4, 1 -> 4,
  28.  1 -> 4, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5, 1 -> 5,
  29.  2 -> 1, 2 -> 3, 2 -> 4, 2 -> 5, 3 -> 1, 3 -> 1, 3 -> 2, 3 -> 2,
  30.  3 -> 2, 3 -> 2, 3 -> 2, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4,
  31.  3 -> 4, 3 -> 4, 3 -> 4, 3 -> 4, 3 -> 5, 3 -> 5, 3 -> 5, 3 -> 5,
  32.  4 -> 1, 4 -> 2, 4 -> 3, 4 -> 5, 5 -> 1, 5 -> 2, 5 -> 3, 5 -> 4} *)
  33.    
  34. Manipulate[
  35.  Row[{GraphPlot[multiedge, MultiedgeStyle -> None, Method -> s],
  36.    GraphPlot[adjMat1, MultiedgeStyle -> None, Method -> s],
  37.    AdjacencyGraph[adjMat1 /. [Infinity] -> 0,
  38.     GraphLayout -> s]}], {s, {"SpringElectricalEmbedding",
  39.    "SpringEmbedding", "HighDimensionalEmbedding", "CircularEmbedding",
  40.     "RandomEmbedding", "LinearEmbedding"}}]
  41.    
  42. Options[VisualizeNetwork] = Options[Graph];
  43. VisualizeNetwork[{adjMat2_, edgecoloring_: {Green, Red},
  44. scaling_: 600, opacity_: 0.5, ShowEdgeWeight_: True},
  45. opts : OptionsPattern[]] :=
  46. Block[{gr, weight, Nweight, edge, nodes, vertexStyle, EdgeBlendColor,edgeStyle, g},
  47. gr = WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True];
  48. (*Lets extract the edge weight list from your graph object*)
  49. weight = AbsoluteOptions[gr, EdgeWeight][[1, 2]];
  50. (*Extract the edge list*)
  51. edge = EdgeList@gr;
  52. (*Extract the vertex list*)
  53. nodes = VertexList@gr;
  54. (* Make a color blend by scaling all the edge weight with the maximum edge weight *)
  55. EdgeBlendColor = Map[ Blend[edgecoloring, #/Max[weight]] &, weight];
  56. (* Make a thickness by scaling all the edge weight with the maximum edge weight *)
  57. edgeStyle =
  58. MapThread[(#1 -> {Opacity[opacity], #3,
  59.      Thickness[#2/scaling]}) &, {edge, weight, EdgeBlendColor}];
  60. Nweight = MapThread[
  61.  Rasterize[#1, ImageSize -> 9,
  62.    Background -> Lighter@#2] &, {weight, EdgeBlendColor}];
  63.  g = If[ShowEdgeWeight == True,
  64.  Graph[nodes, edge, EdgeStyle -> edgeStyle,
  65.   EdgeLabels -> Flatten@MapThread[#1 -> #2 &, {edge, Nweight}],
  66.   VertexLabels ->
  67.    Table[i -> Placed["Name", {1/2, 1/2}], {i, nodes}], opts],
  68.  Graph[nodes, edge, EdgeStyle -> edgeStyle,
  69.   VertexLabels ->
  70.    Table[i -> Placed["Name", {1/2, 1/2}], {i, nodes}], opts]
  71.  ];
  72. g
  73. ];
  74.    
  75. adjMat1 = {{Infinity, 1, 1, 1, 1}, {1, Infinity, 1, 1, 1}, {1, 1,
  76. Infinity, 1, 1}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
  77. adjMat2 = {{Infinity, 8, 6, 3, 7}, {1, Infinity, 1, 1, 1},
  78. {2, 5,Infinity, 9, 4}, {1, 1, 1, Infinity, 1}, {1, 1, 1, 1, Infinity}};
  79. (* Some Graph visualization option *)
  80. opts = {VertexSize -> Medium, VertexSize -> Medium,
  81. VertexStyle ->Directive[Opacity[0.65`], Blue, EdgeForm[None]],
  82. VertexLabelStyle -> Directive[GrayLevel[0], 12],
  83. VertexSize -> {"Scaled", 0.3`},
  84. GraphLayout -> "SpringElectricalEmbedding"};
  85. (* Call the function *)
  86. GraphicsGrid@{{VisualizeNetwork[{adjMat1, {Yellow, Red}, 600, 0.5,
  87.  True}, opts],
  88. VisualizeNetwork[{adjMat2, {Yellow, Red}, 600, 0.5, True}, opts]}}
  89.    
  90. gr = WeightedAdjacencyGraph[adjMat2, DirectedEdges -> True];
  91. weight = AbsoluteOptions[gr, EdgeWeight][[1, 2]];
  92. edge = EdgeList@gr;
  93. k = Graph[edge, EdgeLabels -> (el=MapThread[#1 -> #2 &, {edge, weight}])];
  94. g = (AbsoluteOptions[k,
  95.   EdgeLabels] /. {(a_ [DirectedEdge] b_ -> c_) :>  {a -> b,
  96.     c}})[[1, 2]];
  97.    
  98. GraphPlot[g,
  99.    EdgeRenderingFunction -> ({Text[Style[#3, 15], Mean[#1]], Blue,
  100.    AbsoluteThickness[0.5 + #3/5], Arrowheads[0.02 + #3/170],
  101.    Arrow[#1, 0.075]} &), VertexLabeling -> True]
  102.    
  103. {e, w} = Transpose[g]
  104. Graph[e, EdgeWeight -> w,
  105. EdgeLabelStyle -> Directive[Red, 20, Background -> White],
  106. EdgeLabels -> el ,
  107. EdgeShapeFunction ->
  108.    el /. {((a_ [DirectedEdge] b_) ->  c_) -> ((a [DirectedEdge]
  109.     b) -> ({AbsoluteThickness[.9 + c/4], Arrow[#1, 0.025]} &))}]
  110.    
  111. Graph[{1 <-> 2, 2 <-> 3}, EdgeWeight -> {1, 2},
  112.   GraphLayout -> {"SpringElectricalEmbedding", "EdgeWeighted" -> True}]
  113.    
  114. <<IGraphM`
  115. g = Graph[{1 <-> 2, 2 <-> 3}, EdgeWeight -> {1, 2}];
  116. IGLayoutFruchtermanReingold[g]
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top