2

I'm writing a simple digital simulator for combinatory and sequential circuits and I've chosen a graph approach for modelling this problem.

If the circuit have no loops, I can represent it as a DAG. In this case, I can think of at least two ways to perform a simulation: recursively, going from the outputs backwards or toposorting the graph.

The problem comes when a loop exists as I can't sort nodes anymore, but since a circuit is a directed graph whose edges go from inputs to outputs in only one direction, loops are the only edges "going back".

Hence, with these restrictions, I was looking for a way to perform topological sort "ignoring loops" given the adjacency list.

Here's my idea: if I menage to sort the components regardless of loops, I can simply evaluate loop drivers first and then simulate like if no loops existed. This approach works fine when I solve logic circuits by hand.

In other words, I'd like to somewhat sort the graph pretending that back edges don't exist (hence I have a DAG). Is there a way to do this?

EDIT: take a look at the following picture. enter image description here Here's the adjacency list corresponding to that circuit:

n -> XNOR
m -> AND
AND -> NOT
NOT -> NAND
NAND -> D-FLIPFLOP
XNOR -> NAND
D-FLIPFLOP-> x, AND, XNOR
x -> 

The toposort i'd like to obtain is (for example) this one:

m AND NOT n XNOR NAND D-FLIPFLOP x  

ignoring the two loops

D-FLIPFLOP -> AND
D-FLIPFLOP -> XNOR
Elle
  • 305
  • 2
  • 10
  • Do you want an explanation on how to sort a DAG? Or are you having difficulties pretending that back edges don't exist? – Magma Jul 31 '19 at 14:46
  • I'm having difficulties pretending that back edges don't exist – Elle Jul 31 '19 at 14:58
  • Pick any topological sorting algorithm you like. Replace every mention of "edge" by "edge that isn't a back edge". Implement the resulting algorithm. – Magma Jul 31 '19 at 15:05
  • Your back edges are specially marked, or not? Do you want to know how to decide which edges are back edges? – Magma Jul 31 '19 at 15:06
  • I'd like to have this done in one method that accepts the adj list and sorts the components the way I want then they're not specially marked unless I somewhat do it in this method – Elle Jul 31 '19 at 15:14
  • 1
    Do you have any preferences as to which edges should be marked? If not, just pick any cycle of unmarked edges and mark one of its edges, repeat until there are no cycles of unmarked edges. Then you can topological sort. – Magma Jul 31 '19 at 16:37
  • I'd suggest finding Strongly Connected Components and replacing them with a new single node in your graph. You can find many implementations of this problem on the internet. After that you'll always have DAG, so you can easily use topsort – Maras Aug 01 '19 at 00:06
  • I've just edited my post in order to make it clearer. @Magma Can you please write some code (whatever programming language you like) to explain what you meant? – Elle Aug 01 '19 at 11:04
  • @Elle This code has implemented the method mentioned by Magma https://github.com/USC-SQL/RAT-TRAP/blob/master/source_code/SQLiteDetection/src/sql/global/GlobalFlowAnalysis.java#L306 , and this code has implemented the method mentioned by Maras https://github.com/USC-SQL/RAT-TRAP/blob/d8b44e5112a3fdcd3c4ad8e0022853050d8b5db2/source_code/SQLiteDetection/src/CallGraph/StringCallGraph.java#L291 – coder.chenzhi Jan 08 '20 at 15:32

1 Answers1

3

It seems you're interested in the problem of finding a small feedback arc set. The problem of finding a smallest such set is NP-hard in general, but there's a fast algorithm with decent approximate results by Eades, Lin and Smyth linked in the answer to this question. I'll repeat it here. The goal of the algorithm is to find an ordering of the edges such that relatively few edges run opposite to that ordering, which is also what you're interested in.

  1. You have your graph, a left stack (initially empty), and a right stack (also initially empty).
  2. While the graph has a source (a vertex without incoming edges):
    • Choose a source, disconnect it from the graph and place it on top of the left stack.
  3. While the graph has a sink (a vertex without outgoing edges):
    • Choose a sink, disconnect it from the graph and place it on top of the right stack.
  4. If the graph is not empty:
    • Choose a vertex of the graph such that the number of outgoing edges minus the number of incoming edges is as large as possible.
    • Disconnect that vertex from the graph and place it on top of the left stack.
    • Go to 1.
  5. Take the left stack, flip it and place it on top of the right stack. The right stack, from top to bottom, is the desired ordering.
Magma
  • 566
  • 2
  • 8