# [?] Create a path matrix using Mathematica?

Posted 2 years ago
1928 Views
|
1 Reply
|
2 Total Likes
|
 I have created a code that finds all possible paths from source node (1) to demand node (4), as per the attached file. I need to create a path matrix in which, number of rows is equivalent to the number of paths, and number of columns is equivalent to the number of edges in a network. This matrix is a zero-one matrix with 1 as its (i, j)th entry if the edge is present in the path to the demand node, and 0 as its (i, j)th entry if it is not. For example, the first possible path to get from node (1) to node (4) is {a,e,f}. The corresponding matrix shall be {1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0} and the same procedure goes for the other paths. The final path matrix shall be as the attached image Attachments:
Answer
1 Reply
Sort By:
Posted 2 years ago
 Nehal,One (not very computationally efficient) way to do this paths = {{a, e, f}, {a, b, c}, {a, i, l, j, f}, {a, e, h, g, d}, {a, i, m, q, o, j, f}, {a, i, l, n, k, h, f}, {a, i, l, n, k, g, d}, {a, i, l, j, h, g, d}, {a, e, j, n, k, g, d}, {a, i, m, r, u, t, o, j, f}, {a, i, m, r, u, p, n, j, f}, {a, i, m, r, u, p, k, h, f}, {a, i, m, r, u, p, k, g, d}, {a, i, m, q, t, p, n, j, f}, {a, i, m, q, t, p, k, h, f}, {a, i, m, q, t, p, k, g, d}, {a, i, m, q, o, n, k, h, f}, {a, i, m, q, o, n, k, g, d}, {a, i, m, q, o, j, h, g, d}, {a, i, l, o, t, p, k, h, f}, {a, i, l, o, t, p, k, g, d}, {a, e, j, o, t, p, k, g, d}, {a, i, m, s, v, u, t, o, j, f}, {a, i, m, s, v, u, p, n, j, f}, {a, i, m, s, v, u, p, k, h, f}, {a, i, m, s, v, u, p, k, g, d}, {a, i, m, r, u, t, o, n, k, h, f}, {a, i, m, r, u, t, o, n, k, g, d}, {a, i, m, r, u, t, o, j, h, g, d}, {a, i, m, r, u, p, n, j, h, g, d}, {a, i, m, q, t, p, n, j, h, g, d}, {a, i, l, o, q, r, u, p, k, h, f}, {a, i, l, o, q, r, u, p, k, g, d}, {a, e, j, o, q, r, u, p, k, g, d}, {a, e, j, l, m, r, u, p, k, g, d}, {a, e, j, l, m, q, t, p, k, g, d}, {a, i, m, s, v, u, t, o, n, k, h, f}, {a, i, m, s, v, u, t, o, n, k, g, d}, {a, i, m, s, v, u, t, o, j, h, g, d}, {a, i, m, s, v, u, p, n, j, h, g, d}, {a, i, l, o, q, s, v, u, p, k, h, f}, {a, i, l, o, q, s, v, u, p, k, g, d}, {a, e, j, o, q, s, v, u, p, k, g, d}, {a, e, j, l, m, s, v, u, p, k, g, d}} nodeLabels = paths // Flatten // Union; nodeCount = nodeLabels // Length; indexes = Map[ToString /* LetterNumber, paths, {-1}]; matrix = Map[SparseArray[# -> ConstantArray[1, Length@#]] & /* Normal /* (PadRight[#, nodeCount] &), indexes]; Grid[matrix // Prepend[nodeLabels], Frame -> All, Background -> {Automatic, {1 -> Yellow}}] 
Answer
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments