Exemplo n.º 1
0
//Kruskal’s Minimum Spanning Tree (MST)
void Graph::krusMST() {
    vector<pair< int, pair<int,int> > > ls;
    //sort all the edge
    //input the beginning vertex,end vertex, distance from the private list<int,int> adj
    list<pair<int,int> >::iterator it;
    for(int i=0;i<nVert;i++) {
        for(it=adj[i].begin();it!=adj[i].end();++it) {
            ls.push_back(make_pair((*it).second,make_pair(i,(*it).first)));
        }
    }
    sort(ls.begin(),ls.end());
    vector<pair< int, pair<int,int> > >::iterator it2=ls.begin();
    
    int weight=0;//weight of the tree
    set<int> s;
//Get the minimum spanning tree
    cout<<"The minimum spanning tree: "<<endl;
    for(;it2!=ls.end();++it2) {
        //Determine create circle or not
        if(!isCycle((*it2).second.first,(*it2).second.second)) {
            weight+=(*it2).first;
            s.insert((*it2).second.first);
            s.insert((*it2).second.second);
            cout<<name[(*it2).second.first]<<" --- "<<name[(*it2).second.second];
            cout<<"   "<<(*it2).first<<endl;
        }
    }
    cout<<"Weight: "<<weight<<endl;
}
Exemplo n.º 2
0
/* =============================================================================
 * isCycle
 * =============================================================================
 */
static bool_t
isCycle (vector_t* nodeVectorPtr, net_node_t* nodePtr)
{
    switch (nodePtr->mark) {
        case NET_NODE_MARK_INIT: {
            nodePtr->mark = NET_NODE_MARK_TEST;
            list_t* childIdListPtr = nodePtr->childIdListPtr;
            list_iter_t it;
            list_iter_reset(&it, childIdListPtr);
            while (list_iter_hasNext(&it, childIdListPtr)) {
                long childId = (long)list_iter_next(&it, childIdListPtr);
                net_node_t* childNodePtr =
                    (net_node_t*)vector_at(nodeVectorPtr, childId);
                if (isCycle(nodeVectorPtr, childNodePtr)) {
                    return TRUE;
                }
            }
            break;
        }
        case NET_NODE_MARK_TEST:
            return TRUE;
        case NET_NODE_MARK_DONE:
            return FALSE;
            break;
        default:
            assert(0);
    }

    nodePtr->mark = NET_NODE_MARK_DONE;
    return FALSE;
}
Exemplo n.º 3
0
/* =============================================================================
 * net_isCycle
 * =============================================================================
 */
bool_t
net_isCycle (net_t* netPtr)
{
    vector_t* nodeVectorPtr = netPtr->nodeVectorPtr;
    long numNode = vector_getSize(nodeVectorPtr);
    long n;
    for (n = 0; n < numNode; n++) {
        net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, n);
        nodePtr->mark = NET_NODE_MARK_INIT;
    }

    for (n = 0; n < numNode; n++) {
        net_node_t* nodePtr = (net_node_t*)vector_at(nodeVectorPtr, n);
        switch (nodePtr->mark) {
            case NET_NODE_MARK_INIT:
                if (isCycle(nodeVectorPtr, nodePtr)) {
                    return TRUE;
                }
                break;
            case NET_NODE_MARK_DONE:
                /* do nothing */
                break;
            case NET_NODE_MARK_TEST:
                assert(0);
                break;
            default:
                assert(0);
                break;
        }
    }

    return FALSE;
}
Exemplo n.º 4
0
// Driver program to test above functions
int main()
{
    /* Let us create following graph
         0
        |  \
        |    \
        1-----2 */
    int V = 3, E = 3;
    struct Graph* graph = createGraph(V, E);

    // add edge 0-1
    graph->edge[0].src = 0;
    graph->edge[0].dest = 1;

    // add edge 1-2
    graph->edge[1].src = 1;
    graph->edge[1].dest = 2;

    // add edge 0-2
    graph->edge[2].src = 0;
    graph->edge[2].dest = 2;

    if (isCycle(graph))
        printf( "graph contains cycle" );
    else
        printf( "graph doesn't contain cycle" );

    return 0;
}
Exemplo n.º 5
0
void isCycle(Node const& n, VisitedNodes& stack) {
  if (stack.find(n.getPath()) != stack.cend()) {
    LOG(INFO) << "loop";
    Exception e(__func__, __FILE__, __LINE__, EINVAL, "LOOP DETECTED IN GRAPH");
    std::string message = " +-> " + n.getPath();
    throw Exception(e.getErrorMessage(),
                    __func__, __FILE__, __LINE__,
                    e.getCode(), message.c_str());
  }

  Rule const* child = n.getChild();
  if (child == nullptr) {
    return;
  }

  auto pos = stack.insert(n.getPath());

  NodeArray const& inputs = child->getInputs();
  for (auto it = inputs.cbegin(); it != inputs.cend(); it++) {
    try {
      isCycle(**it, stack);
    } catch (Exception& e) {
      std::string message = " |   "+ n.getPath();
      throw Exception(e.getErrorMessage(), __func__, __FILE__, __LINE__,
                      e.getCode(), message.c_str());
    }
  }

  stack.erase(pos.first);
}
Exemplo n.º 6
0
void checkGraphLoop(Graph const& g) {
  NodeSet const& roots = g.getRoots();

  for (auto it = roots.cbegin(); it != roots.cend(); it++) {
    VisitedNodes stack;

    isCycle(**it, stack);
  }
}
Exemplo n.º 7
0
// Driver program to test above functions
int main()
{
    /* Let us create following graph
         0
        |  \
        |    \
        1-----2 */
 
    int V = 10, E = 9;
    struct Graph* graph = createGraph(V, E);
 
    // add edge 0-1
    graph->edge[0].src = 3;
    graph->edge[0].dest = 1;
 
    // add edge 1-2
    graph->edge[1].src = 3;
    graph->edge[1].dest = 2;
 
    // add edge 0-2
    graph->edge[2].src = 4;
    graph->edge[2].dest = 0;

    graph->edge[3].src = 4;
    graph->edge[3].dest = 3;

    graph->edge[4].src = 6;
    graph->edge[4].dest = 7;

    graph->edge[5].src = 6;
    graph->edge[5].dest = 8;

    graph->edge[6].src = 9;
    graph->edge[6].dest = 5;
    
    graph->edge[7].src = 9;
    graph->edge[7].dest = 6;

    graph->edge[8].src = 9;
    graph->edge[8].dest = 4;
 
    if (isCycle(graph))
        printf( "Graph contains cycle" );
    else
        printf( "Graph doesn't contain cycle" );
 
    return 0;
}
short ElemDDLSGOptions::genSGA(SequenceGeneratorAttributes &sga)
{
  sga.setSGStartValue(getStartValue());
  sga.setSGIncrement(getIncrement());
  sga.setSGMinValue(getMinValue());
  sga.setSGMaxValue(getMaxValue());

  sga.setSGCache(getCache());
  sga.setSGCycleOption(isCycle());

  sga.setSGFSDataType(getFSDataType());

  sga.setSGResetOption(isReset());

  return 0;
}
Exemplo n.º 9
0
void map(void)
{
    int i, j, c;
    uint16_t f1, f2;
    bool cycle;
    uint8_t *keys;

    c = 0;

    do {
        initGraph();
        cycle = false;

        // Randomly generate T1 and T2
        for (i = 0; i < SetSize * EntryLen; i++) {
            T1base[i] = rand() % NumVert;
            T2base[i] = rand() % NumVert;
        }

        for (i = 0; i < NumEntry; i++) {
            f1 = 0;
            f2 = 0;
            getKey(i, &keys);
            for (j = 0; j < EntryLen; j++) {
                T1 = T1base + j * SetSize;
                T2 = T2base + j * SetSize;
                f1 += T1[keys[j] - SetMin];
                f2 += T2[keys[j] - SetMin];
            }
            f1 %= (uint16_t)NumVert;
            f2 %= (uint16_t)NumVert;
            if (f1 == f2) { // A self loop. Reject!
                printf("Self loop on vertex %d!\n", f1);
                cycle = true;
                break;
            }
            addToGraph(numEdges++, f1, f2);
        }

        if (cycle || (cycle = isCycle())) // OK - is there a cycle?
            printf("Iteration %d\n", ++c);
        else
            break;

    } while (/* there is a cycle */ 1);
}
// Driver program to test above functions
int main()
{
    /* Let us create following graph
         0
        |  \
        |    \
        1-----2 */
    struct Graph* graph = createGraph(6, 6);
 
    // add edge 0-1
    graph->edge[0].src = 0;
    graph->edge[0].dest = 1;
 
    // add edge 1-2
    graph->edge[1].src = 1;
    graph->edge[1].dest = 2;
 
    // add edge 1-4
    graph->edge[2].src = 1;
    graph->edge[2].dest = 4;

    // add edge 2-3
    graph->edge[3].src = 2;
    graph->edge[3].dest = 3;
 
    // add edge 3-2
    graph->edge[4].src = 3;
    graph->edge[4].dest = 2;

    // add edge 4-5
    graph->edge[5].src = 4;
    graph->edge[5].dest = 5;

    if (isCycle(graph))
        printf( "Graph contains cycle" );
    else
        printf( "Graph doesn't contain cycle" );
 
    return 0;
}