Your algorithm could resemble graph theory. I understand the hierarchical lists above are shown for simplicity. Given your requirements, the limits of a hierarchical structure are revealed!
The nodes need to be managed before they can be processed. My approach to this problem, not having a background in graph structures, would be to review the material on graph structures first, for example this this and this and get my data encoded using them. Once that's done then process and analyze the structures, for example, create a hash counting the parents, sort the list by the count and generate a new hierarchical representation without changing the underlying graph.
for example, borrowing from the code in the first link, in python3
class Vertex:
def __init__(self, key):
self.id = key
self.connected_to = {}
def add_neighbor(self, nbr, weight=0):
self.connected_to[nbr] = weight
def __str__(self):
return str(self.id) + ' connected_to: ' + str([x.id for x in self.connected_to])
def get_connections(self):
return self.connected_to.keys()
def get_id(self): return self.id
def get_weight(self, nbr): return self.connected_to[nbr]
class Graph:
def __init__(self):
self.vert_list = {}
self.num_vertices = 0
def add_vertex(self, key):
self.num_vertices += 1
new_vertex = Vertex(key)
self.vert_list[key] = new_vertex
return new_vertex
def get_vertex(self, n):
if n in self.vert_list:
return self.vert_list[n]
else:
return None
def __contains__(self, n): return n in self.vert_list
def add_edge(self, f, t, cost=0):
if f not in self.vert_list:
self.add_vertex(f)
if t not in self.vert_list:
self.add_vertex(t)
self.vert_list[f].add_neighbor(self.vert_list[t], cost)
def get_vertices(self): return self.vert_list.keys()
def __iter__(self): return iter(self.vert_list.values())
# the main graph
g = Graph()
# populate the main graph
g.add_vertex('dinner')
g.add_edge('dinner', 'food')
g.add_edge('dinner', 'fork')
g.add_edge('dinner', 'spoon')
g.add_vertex('breakfast')
g.add_edge('breakfast', 'food')
g.add_edge('breakfast', 'fork')
print("\nThe main graph is:")
for v in g:
for c in v.get_connections():
print("( %s , %s )" % (v.get_id(), c.get_id()))
# flip the graph by populating a new one with the main's children
flipped_g = Graph()
for v in g:
for c in v.get_connections():
flipped_g.add_edge(c.get_id(), v.get_id())
print("\nThe main graph inverted is:")
for v in flipped_g:
for c in v.get_connections():
print("( %s , %s )" % (v.get_id(), c.get_id()))
produces the following output:
The main graph is:
( breakfast , food )
( breakfast , fork )
( dinner , food )
( dinner , fork )
( dinner , spoon )
The main graph inverted is:
( food , breakfast )
( food , dinner )
( spoon , dinner )
( fork , breakfast )
( fork , dinner )
This is certainly not a complete solution but it is a framework you can use to get the information you need.
Bear in mind that the add_vertex()
method checks the list of connections and only adds a node if it isn't already there. For example, there is only one dinner
node and only one fork
node.
Here are all the vertices as retrieved with: flipped_g.get_vertices()
dict_keys(['food', 'spoon', 'fork', 'dinner', 'breakfast'])