1

Hello I am iterating a dot file in python using pydot package. I want to color all nodes and edges in a graph who are a parent of particular node unto the starting root. the images below describes the goal.

Actual Graph is following

The actual graph

Required Output assuming given node is _ZL3WTFv

Required goal

Python code is following

import pydot
import sys

def Clean(inp):
    return inp.replace('"{', '').replace('}"', '')

def GetNode(path):
    graph = pydot.graph_from_dot_file(path)
    graph = graph[0]
    nodesList = graph.get_nodes() 
    for e in nodesList:
        tempAttr = e.get_attributes()
        print('Checking', Clean(tempAttr['label']))
        if Clean(tempAttr['label']) == '_ZL3WTFv':
            #how to mark all nodes & paths to the top level parents from here e.g. _ZL3WTFv          
            print('Node Found!')
            break

if len(sys.argv) > 1:
    GetNode(sys.argv[1])
else:
    print('Dot file path Missing!')

The graphviz dot file code is following

digraph "Call graph" {
    label="Call graph";

    Node0x163ebe0 [shape=record,label="{external node}"];
    Node0x163ebe0 -> Node0x163b270;
    Node0x163ebe0 -> Node0x1605360;
    Node0x163ebe0 -> Node0x16052c0;
    Node0x163ebe0 -> Node0x160e2a0;
    Node0x163ebe0 -> Node0x160e600;
    Node0x163ebe0 -> Node0x160eb00;
    Node0x160eb00 [shape=record,label="{_GLOBAL__sub_I_test.cpp}"];
    Node0x160eb00 -> Node0x163b200;
    Node0x163b200 [shape=record,label="{__cxx_global_var_init}"];
    Node0x163b200 -> Node0x163b270;
    Node0x163b200 -> Node0x16052c0;
    Node0x163b270 [shape=record,label="{_ZNSt8ios_base4InitC1Ev}"];
    Node0x163b270 -> Node0x163e480;
    Node0x1605360 [shape=record,label="{_ZNSt8ios_base4InitD1Ev}"];
    Node0x1605360 -> Node0x163e480;
    Node0x16052c0 [shape=record,label="{__cxa_atexit}"];
    Node0x16052c0 -> Node0x163e480;
    Node0x160e2a0 [shape=record,label="{main}"];
    Node0x160e2a0 -> Node0x160e310;
    Node0x160e310 [shape=record,label="{_ZL1Av}"];
    Node0x160e310 -> Node0x160e3b0;
    Node0x160e310 -> Node0x160e450;
    Node0x160e3b0 [shape=record,label="{_ZL1Bv}"];
    Node0x160e3b0 -> Node0x160e3e0;
    Node0x160e450 [shape=record,label="{_ZL1Gv}"];
    Node0x160e450 -> Node0x160e530;
    Node0x160e3e0 [shape=record,label="{_ZL1Cv}"];
    Node0x160e3e0 -> Node0x160e5d0;
    Node0x160e3e0 -> Node0x160e670;
    Node0x160e530 [shape=record,label="{_ZL1Mv}"];
    Node0x160e530 -> Node0x160e850;
    Node0x160e5d0 [shape=record,label="{_ZL3WTFv}"];
    Node0x160e5d0 -> Node0x160e600;
    Node0x160e670 [shape=record,label="{_ZL1Xv}"];
    Node0x160e670 -> Node0x160e750;
    Node0x160e600 [shape=record,label="{_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc}"];
    Node0x160e600 -> Node0x163e480;
    Node0x160e750 [shape=record,label="{_ZL1Yv}"];
    Node0x160e750 -> Node0x160e5d0;
    Node0x160e850 [shape=record,label="{_ZL1Nv}"];
    Node0x160e850 -> Node0x160e8f0;
    Node0x160e8f0 [shape=record,label="{_ZL1Ov}"];
    Node0x160e8f0 -> Node0x160e990;
    Node0x160e990 [shape=record,label="{_ZL1Pv}"];
    Node0x160e990 -> Node0x160ea30;
    Node0x160ea30 [shape=record,label="{_ZL1Sv}"];
    Node0x160ea30 -> Node0x160e5d0;
}

the main problem is I am able to iterate graph but can't mark the path from root to the required node

Zain Ul Abidin
  • 2,467
  • 1
  • 17
  • 29

1 Answers1

2

I don't use the Python interface, but on a quick read this should work:
write a recursive function that uses gv.firstin and gv.nextin to chase the incoming edges back towards the root/parent node. start at your target node and off you go. Color each node and edge as you go. Also keep track of the nodes so you don't get caught in loops

sroush
  • 5,375
  • 2
  • 5
  • 11
  • Your this technique helped a lot and now using the recursion colors are being marked correctly. – Zain Ul Abidin Jun 22 '22 at 13:18
  • @ZainUlAbidin Could you please post an answer with the complete code you ended up with? I would like to accomplish the same thing as your OP and haven't found a simple solution yet. Or if you happen to have a Git(Lab|Hub) account, post it there and I can contribute whichever improvements I come up with based on your code? Thanks! – scottkosty Dec 30 '22 at 16:07
  • Hi @scottkosty glad to hear from you happy to help, https://github.com/Zedstron this is my GitHub profile address, feel free to contact me either email or my WhatsApp number both are listed there, I have not uploaded that code in a repo as it was for private project, but I can share with you that particular peace to assist you. – Zain Ul Abidin Dec 31 '22 at 08:35
  • @ZainUlAbidin thank you! I just sent you an email. I appreciate the help! – scottkosty Dec 31 '22 at 17:44