4
$\begingroup$

Suppose that we have two matrices with identical dimensions: matA and matB. I want to identify a set of pathways using matA, but then map the respective edge weights from matB onto the pathways identified from matA. I like to perform this operation with a MMA function such as map[matA_, matB_, t1_,t2_]:=

ClearAll[n, matA, matB, map]; << IGraphM`; << BoolEval`; SeedRandom[4]; n = 10; matA = RandomReal[{0.1, 0.5}, {n, n}]; matB = RandomReal[{0.1, 0.4}, {n, n}]; map[matrixA_?MatrixQ, matrixB_?MatrixQ, t1_Real, t2_Real] := { selectBetween[t1, t2] = BoolEval[t1 <= matrixA < t2]; subgraphBetween[t1, t2] = AdjacencyGraph[selectBetween[t1, t2], VertexLabels -> "Name"]; pindex = FindPath[subgraphBetween[t1, t2], 1, 3, Infinity]; prule=Rule @@@ Partition[pindex[[4]], 2, 1] Graph[prule, EdgeWeight -> Flatten[matrixB], DirectedEdges->True, VertexLabels -> "Name", EdgeLabels -> "EdgeWeight"] 

I think everything works fine until Graph[..] in the above code. pindex is identified but I cannot map the associated edge weights from matB onto pindex.

Needless to say, any improvement on this code is most welcome. Thank you for your help.

$\endgroup$

2 Answers 2

4
$\begingroup$

Update: "give different color to source (red) and sink (green) vertices"

ClearAll[map3] map3[matrixA_?MatrixQ, matrixB_?MatrixQ, t1_Real, t2_Real] := Module[{indices = Union @@ (Partition[#, 2, 1] & /@ FindPath[AdjacencyGraph[Map[Boole[t1 <= # <= t2] &, matrixA, {-1}]], #[[1]], #2[[1]], ∞, All])}, HighlightGraph[Graph[DirectedEdge @@@ indices, EdgeWeight -> Extract[matrixB, indices], EdgeLabels -> "EdgeWeight", ##3], {#, #2}]] &; 

Examples:

map3[matA, matB, .2, .3][{4}, {6}, VertexLabels -> "Name"] 

enter image description here

map3[matA, matB, .2, .3][Style[4, Red], Style[6, Green], VertexLabels -> "Name", VertexSize -> {4 | 6 -> Large}, PerformanceGoal -> "Quality", ImagePadding -> 20] 

enter image description here

Original answer:

Maybe something like:

ClearAll[map2] map2[matrixA_?MatrixQ, matrixB_?MatrixQ, t1_Real, t2_Real] := Module[{indices = Union @@ (Partition[#, 2, 1] & /@ FindPath[AdjacencyGraph[ Map[Boole[t1 <= # <= t2] &, matrixA, {-1}]], #, #2, ∞, All])}, HighlightGraph[Graph[DirectedEdge @@@ indices, EdgeWeight -> Extract[matrixB, indices], EdgeLabels -> "EdgeWeight", ##3], {#, #2}]] &; 

Examples:

SeedRandom[4]; n = 10; matA = RandomReal[{0.1, 0.5}, {n, n}]; matB = RandomReal[{0.1, 0.4}, {n, n}]; map2[matA, matB, .2, .3][4, 6, VertexLabels -> "Name"] 

enter image description here

vLabels = {1 -> AGF, 2 -> CO12, 3 -> MA1, 4 -> MA2, 5 -> EGW, 6 -> CST, 7 -> WHS, 8 -> HOT, 9 -> TSC, 10 -> FIN}; map2[matA, matB, .2, .3][4, 6, VertexLabels -> {v_ :> Placed[v /. vLabels, Center]}, VertexSize -> Large] 

enter image description here

Row[map2[matA, matB, .2, .3][##, ImageSize -> Medium, VertexLabels -> Placed["Name", Center], VertexSize -> Large] & @@@ {{4, 6}, {1, 4}}, Spacer[10]] 

enter image description here

$\endgroup$
6
  • $\begingroup$ Yes, it is exactly what I wanted. Thank you... $\endgroup$ Commented Jan 27, 2020 at 22:04
  • $\begingroup$ in the above code I wanted to use my custom-made vertex labels such as vLabels = {1 -> AGF, 2 -> CO12, 3 -> MA1, 4 -> MA2, 5 -> EGW, 6 -> CST, 7 -> WHS, 8 -> HOT, 9 -> TSC, 10 -> FIN} but then I receive an error relating to HighlightGraph. I tried several different ways to use my own labels with no success. Could you please tell me where to revise in your code? $\endgroup$ Commented Jan 28, 2020 at 16:58
  • 1
    $\begingroup$ @Tugrul, try map2[matA, matB, .2, .3][4, 6, VertexLabels -> {v_ :> Placed[v /. vLabels, Center]}, VertexSize -> Large]? (If it does not work maybe you can post a new question with the errors you are getting.) $\endgroup$ Commented Jan 28, 2020 at 18:08
  • $\begingroup$ In the graph just above, given a source and a sink we derive the subgraph from source to sink. Can we give different color to source (red) and sink (green) vertices to clearly show the starting and ending vertices? $\endgroup$ Commented Jul 30, 2020 at 22:36
  • 1
    $\begingroup$ @TugrulTemel, please see the update. $\endgroup$ Commented Jul 31, 2020 at 2:44
2
$\begingroup$

Here is what I could do to answer my own question:

ClearAll[n, matA, matB, map]; << IGraphM`; << BoolEval`; SeedRandom[4]; n = 10; matA = RandomReal[{0.1, 0.5}, {n, n}]; matB = RandomReal[{0.1, 0.5}, {n, n}]; map[matrixA_?MatrixQ, matrixB_?MatrixQ, lb_Real, ub_Real] := Module[{matA = matrixA, matB = matrixB, t1 = lb, t2 = ub, subgraphBetween, pindex, prule, paths}, subgraphBetween[t1, t2] := AdjacencyGraph[BoolEval[t1 <= matA < t2], VertexLabels -> "Name"]; pindex = FindPath[subgraphBetween[t1, t2], 4, 6, Infinity, All]; prule = DeleteDuplicates[ Flatten@Table[ Rule @@@ Partition[pindex[[i]], 2, 1], {i, Length[pindex]} ]]; paths = Graph[prule, EdgeWeight -> Take[Flatten@matB, Length[prule]], VertexLabels -> "Name", EdgeLabels -> "EdgeWeight"] ] map[matA, matB, 0.2, 0.3] 

Although this partially answers my question, it is not so elegant. Partial because the respective elements from matB are not selected. I just selected an equal number of edge weights to have a working code. The correct selection of cells from matB should match with the edges in prule.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.