CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Threaded View

  1. #1
    Join Date
    Apr 2014
    Posts
    6

    Creating Matrix/map for Topological sort?

    I am trying to simulate the logical circuits having NAND gates. I am reading the structure from .bench file. The structure of a circuit looks like below:

    Code:
    INPUT(1)
    INPUT(2)
    INPUT(3)
    INPUT(6)
    INPUT(7)
    
    
    10 = NAND(1, 3)
    11 = NAND(3, 6)
    16 = NAND(2, 11)
    19 = NAND(11, 7)
    22 = NAND(10, 16)
    23 = NAND(16, 19)
    It can be seen that the inputs for Gate 22 are 10 and 16 which are also NAND gates. On the other hand, for gate 10, the inputs are 1 and 3 which are simple inputs. So, i need to sort the gates topologically. Although topological sort will not affect the output for this particular example but i have big circuits also in which topological sort is required. But we can use this simple example to do the topological sort.

    My effort:

    I thought to create a map which will keep the information about the gate name and its indegree. But i am unable to proceed further.

    Code:
    void Circuit::topologicalSort()			//topological sorting (for the mixed circuits)
    {
    
    	int counter = 0;
    	std::map<string, int> unsortTopoList; //this list keeps a track of gate name and its Indegree
    	
    	for(int i=0; i<gates.size(); i++)
    	{	
    		string gate_name = gates[i]->getName();
    		int INDEGREE = 0;
    		for(int j=0; j<gates[i]->getNumInputs(); j++)
    		{
    			string gate_input_name = gates[i]->getInput(j)->getName();
    			int result = ( findGate(gate_input_name) ); // We trying to check if the given gate's input is also a gate or not.
    			INDEGREE = INDEGREE + result;
    			result = 0;
    		}
    		
    		unsortTopoList[ gates[i]->getName() ] = INDEGREE; //This list will tell that the indegree of gate name "gate_input_vec[i][0]"
    		counter++;
    	}
    
    	for(int i=0; i<counter; i++)
    	{
    		cout<<"\nGate: "<<gates[i]->getName()<<"  is having Indegree: "<<unsortTopoList[ gates[i]->getName() ];
    	}
    
    	cout<<"\n";
    }
    
    int Circuit::findGate( string gateName )
    {
    	int result = 0;
    
    	for(int i=0; i<gates.size(); i++)
    	{
    		if( gateName.compare( gates[i]->getName() )  ==0 ) //"==0" means that they are same.
    		{
    			result= 1;
    			break;
    		}
    		else
    			result= 0;
    	}
    	
    	return result;
    }
    Last edited by siddmittal; May 10th, 2014 at 02:46 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured