I'm trying to implement Hierholzers Algorithm, in Python, to find a Eulerian Cycle in a directed graph. You can find an example of the algorithm here, Hierholzers Algorithm.
In terms of the example, i have just completed the fifth stage, in other words my algorithm produces a list of levels, where each level represents a tour in the graph.list_of_levels = [[0, 3, 2, 1, 0], [2, 6, 5, 4, 2], [6, 8, 7, 9, 6]]
To complete the process, i need to combine these lists, by inserting each level to its upper level, at the appropriate position. For example, the steps for the list above would be,
Step 1 list_of_levels = [[0, 3, 2, 1, 0], [2, 6, 8, 7, 9, 6, 5, 4, 2]]
Step 2 list_of_levels = [[0, 3, 2, 6, 8, 7, 9, 6, 5, 4, 2, 1, 0]]
So far, i have tried Python's insert(index, obj) method but the result contains the brackets of the inserted list and also it doesn't replace the item, in the index position, with the inserted item. The corresponding result with insert method, for Step 1, look like this.[2, [6, 8, 7, 9, 6], 6, 5, 4, 2]
So the question is how to unify those levels without keeping the brackets and without ending up with duplicate items (vertices) from other levels.
I'm thinking of manually delete the vertex wherever i insert the next level and after i finish with all levels, flat the final list, although for some reason i couldn't make chain from iterable to work.
Even if i manage to implement this solution, i'm sure there is a better alternative.
I'd be happy to see some ideas.
You can insert a list of elements into a list in the following way:
B = [2, 6, 5, 4, 2]
C = [6, 8, 7, 9, 6]
B[1:2] = C
print B
prints
[2, 6, 8, 7, 9, 6, 5, 4, 2]
Note that this operation has both deleted the original 6 and inserted the new elements in place of the 6.
This approach is described in the documentation as:
s[i:j] = t
slice of s from i to j is replaced by the contents of the iterable t
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With