I have some directed graph data where each each row represents one edge. I need to convert such graph data to tree data with python. For example my looks like this:
[{'source': 'a','target': 'b'},
{'source': 'b','target': 'a'}
{'source': 'a','target': 'c'}
{'source': 'c','target': 'd'}
{'source': 'd','target': 'a'}
{'source': 'z','target': 'a'}]
Here the first node would be 'a'. Since 'b' has an edge with 'a', it resembles a cycle and in this case I want to rename it like 'a-prime' let's say. Also 'a' can have an edge with 'c' and 'c' have an edge with 'd'and the 'd' can send back to 'a' or even 'c' and again there is a cycle, so the last 'd' will be renamed as 'd-prime'. Last case would be when some random node appears like 'z' with 'a', but that one is fine as it is as it doesn't resemble a cycle. Here is what I tried:
tmp =[]
for x in data:
tmp.append(x['source'])
if x['target'] in tmp:
x['target'] = x['target'] + '-prime'
But I realized this handles first case only and then I got stuck. I need somehow to trace each cycle..