OpenRAM/compiler/base/graph_util.py

69 lines
2.1 KiB
Python
Raw Normal View History

2019-04-19 10:27:06 +02:00
import os
from collections import defaultdict
import gdsMill
import tech
import math
import globals
import debug
from vector import vector
from pin_layout import pin_layout
class graph():
"""Implements a directed graph"""
def __init__(self):
self.graph = defaultdict(list)
def add_edge(self, u, v):
"""Adds edge to graph. Nodes added as well if they do not exist."""
if v not in self.graph[u]:
self.graph[u].append(v)
def add_node(self, u):
"""Add node to graph with no edges"""
if not u in self.graph:
self.graph[u] = []
def remove_edges(self, node):
"""Helper function to remove edges, useful for removing vdd/gnd"""
self.graph[node] = []
def print_all_paths(self,s, d):
2019-04-19 10:27:06 +02:00
# Mark all the vertices as not visited
visited = set()
# Create an array to store paths
path = []
self.path_count = 0
2019-04-19 10:27:06 +02:00
# Call the recursive helper function to print all paths
self.print_all_paths_util(s, d,visited, path)
debug.info(1, "Paths found={}".format(self.path_count))
2019-04-19 10:27:06 +02:00
def print_all_paths_util(self, u, d, visited, path):
2019-04-19 10:27:06 +02:00
# Mark the current node as visited and store in path
visited.add(u)
path.append(u)
# If current vertex is same as destination, then print
# current path[]
if u == d:
debug.info(1,"{}".format(path))
self.path_count+=1
2019-04-19 10:27:06 +02:00
else:
# If current vertex is not destination
#Recur for all the vertices adjacent to this vertex
for i in self.graph[u]:
if i not in visited:
self.print_all_paths_util(i, d, visited, path)
2019-04-19 10:27:06 +02:00
# Remove current vertex from path[] and mark it as unvisited
path.pop()
visited.remove(u)
def __str__(self):
""" override print function output """
return "Nodes: {}\nEdges:{} ".format(list(self.graph), self.graph)