Advertisement
in2erval

Non-backtracking line graph example

Sep 26th, 2022 (edited)
571
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 5.75 KB | Source Code | 0 0
  1. #=
  2. Non-backtracking line graph's adjacency matrix and row/column-selecting matrices, based on https://tatsuya.haddow.dev/articles/the-exact-non-backtracking-walk-problem-part-1. You can copy the entire contents of this file into Julia's REPL to set up the definitions in your session.
  3.  
  4. The adjacency matrix is laid out in alphabetical order of each edge's traversal directions. Based on how the non-backtracking line graph is constructed, an entry in the matrix is 1 if the edge traversal denoted by the row can be followed by the edge traversal denoted by the column, and 0 otherwise. For example, A->B can be followed by B->C, B->E, and B->G, but not B->A as that would be backtracking:
  5.  
  6.   AB BA BC BE BG CB CD DC DF EB EF FD FE FI GB GH HG HI IF IH
  7. AB 0  0  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  8. BA 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  9. BC 0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0
  10. BE 0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0
  11. BG 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0
  12. CB 0  1  0  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  13. CD 0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0
  14. DC 0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  15. DF 0  0  0  0  0  0  0  0  0  0  0  0  1  1  0  0  0  0  0  0
  16. EB 0  1  1  0  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  17. EF 0  0  0  0  0  0  0  0  0  0  0  1  0  1  0  0  0  0  0  0
  18. FD 0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0  0  0
  19. FE 0  0  0  0  0  0  0  0  0  1  0  0  0  0  0  0  0  0  0  0
  20. FI 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1
  21. GB 0  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
  22. GH 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0
  23. HG 0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0  0  0
  24. HI 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0
  25. IF 0  0  0  0  0  0  0  0  0  0  0  1  1  0  0  0  0  0  0  0
  26. IH 0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  1  0  0  0
  27.  
  28. The row/column-selecting matrices simply selects specific rows from the adjacency matrix. Ones that start with "from_" will choose all rows that start with the given vertex (e.g. from_C chooses CB and CD), and ones that start with "to_" will choose all columns that end with the given vertex (e.g. to_B chooses AB, CB, EB, and GB).
  29.  
  30. To evaluate non-backtracking walks of length k, raise A_NB to the power of k-1 (see the article for the explanation). From there you can left-multiply by the "from_" matrix and right-multiply by the "to_" matrix to determine walks from/to specific vertices:
  31.  
  32. julia> # Walk of length 11 from B to D
  33. julia> from_B * A_NB^10 * to_D
  34. 4×2 Matrix{Int64}:
  35. 0  0
  36. 0  0
  37. 0  0
  38. 0  1
  39.  
  40. The resulting matrix is a trimmed-down version of the adjacency matrix, selecting only rows and columns relevant to the chosen vertices. For the example above, it can be understood as:
  41.        end with... C->D F->D
  42. start with... B->A  0    0
  43.              B->C  0    0
  44.              B->E  0    0
  45.              B->G  0    1
  46. =#
  47.  
  48. A_NB = [0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0; 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0; 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0; 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