int solve() { Graph g; int path[SIZE], length, n, i; int source, target; source = 1; target = 3; if((g = createGraph()) == NULL) { return 1; } addVertex(g, 1, "Blah"); addVertex(g, 2, "Blah blah"); addVertex(g, 3, "Blah blah"); addVertex(g, 4, "Blah blah"); addVertex(g, 5, "Blah blah"); addVertex(g, 6, "Blah blah"); // addVertex(g, 7, "Blah blah"); addEdge(g, 1, 2, 1); addEdge(g, 1, 4, 1); addEdge(g, 2, 3, 1); addEdge(g, 4, 6, 1); addEdge(g, 5, 6, 1); addEdge(g, 6, 3, 1); // addEdge(g, 3, 4, 1); if(checkCycle(g)) { printf("The graph has a cycle\n"); } else { printf("The graph has no cycle\n"); } int weight; weight = shortestPath(g, source, target, path, &length); if(weight == INFINITIVE_VALUE) printf("No path between %d and %d\n", source, target); else { printf("Path between %d and %d\n", source, target); for(i = 0; i < length; i++) printf("%4d\n", path[i]); printf("Total weight: %d\n", weight); } dropGraph(g); return 0; }
bool Node::checkCycle( const NodeRef &sourceNode, const NodeRef &destNode ) const { if( sourceNode == destNode ) return true; if( sourceNode->supportsCycles() || destNode->supportsCycles() ) return false; for( const auto &input : sourceNode->getInputs() ) { if( checkCycle( input, destNode ) ) return true; } return false; }
void Node::connect( const NodeRef &output ) { // make a reference to ourselves so that we aren't deallocated in the case of the last owner // disconnecting us, which we may need later anyway NodeRef thisRef = shared_from_this(); if( ! output || ! output->canConnectToInput( thisRef ) ) return; if( checkCycle( thisRef, output ) ) throw NodeCycleExc( thisRef, output ); mOutputs.push_back( output ); // set output first, so that it is visible in configureConnections() output->connectInput( thisRef ); output->notifyConnectionsDidChange(); }
ELinkNodesResult NodeTree::linkNodes(SocketAddress from, SocketAddress to) { // Check if given addresses are OK if(!validateLink(from, to)) return ELinkNodesResult::InvalidAddress; // Check if we are not trying to link input socket with more than one output bool alreadyExisingConnection = std::any_of(std::begin(_links), std::end(_links), [&](const NodeLink& link) { return link.toNode == to.node && link.toSocket == to.socket; }); if(alreadyExisingConnection) return ELinkNodesResult::TwoOutputsOnInput; // Try to make a connection _links.emplace_back(NodeLink(from, to)); // Keep links sorted std::sort(std::begin(_links), std::end(_links)); // Check for created cycle(s) if(checkCycle(to.node)) { // Look for given node link NodeLink nodeLinkToFind(from, to); auto iter = std::lower_bound(std::begin(_links), std::end(_links), nodeLinkToFind); assert(iter != std::end(_links)); if(iter != std::end(_links)) { _links.erase(iter); // Keep links sorted std::sort(std::begin(_links), std::end(_links)); } return ELinkNodesResult::CycleDetected; } // tag affected node tagNode(to.node); return ELinkNodesResult::Ok; }
/* 注意:这里要保证没有循环连接 */ void Framework::save( MyGUI::xml::ElementPtr node ) { if( !mName.empty() ) node->addAttribute("name",mName); //这里做检测保证框架没有循环连接的铰链 if(checkCycle()) { WARNING_LOG("Framework have cycle."); return; } RigidPtr body = getBodyRigid(); if(body) { MyGUI::xml::ElementPtr child = node->createChild("body"); saveRigid(child, body,nullptr); } }
bool QScriptValueImpl::detectedCycle() const { QHash<QScriptObject*, int> dfn; dfs(m_object_value, dfn, 0); return checkCycle(m_object_value, dfn); }
int solve() { Graph g; int output[SIZE], n, i; if((g = createGraph()) == NULL) { return 1; } addVertex(g, 1, "1"); addVertex(g, 2, "2"); addVertex(g, 3, "3"); addVertex(g, 4, "4"); addVertex(g, 5, "5"); addVertex(g, 6, "6"); addVertex(g, 7, "7"); // addVertex(g, 8, "8"); // addVertex(g, 9, "9"); // addVertex(g, 10, "10"); // addVertex(g, 11, "11"); // addVertex(g, 12, "12"); addEdge(g, 1, 2, 1); // addEdge(g, 2, 3, 1); // addEdge(g, 3, 1, 1); // addEdge(g, 1, 4, 1); // addEdge(g, 5, 1, 1); addEdge(g, 7, 1, 1); addEdge(g, 2, 5, 1); addEdge(g, 6, 3, 1); addEdge(g, 3, 2, 1); // addEdge(g, 7, 12, 1); // addEdge(g, 1, 5, 1); addEdge(g, 4, 5, 1); if(checkCycle(g)) { printf("Cycle here\n"); } else { printf("No cycle here\n"); } // printf("%s\n", getVertex(g, 7)); // printf("The value of %s is %d\n", "11", getKeyByVal(g, "11")); // n = indegree(g, 1, output); // printf("In degree list:\n"); // for(i = 0; i < n; i++) { // printf("%d\n", output[i]); // } // n = outdegree(g, 1, output); // printf("Out degree list:\n"); // for(i = 0; i < n; i++) { // printf("%d\n", output[i]); // } // printf("The number of components: %d\n", getComponents(g)); DAG(g); dropGraph(g); return 0; }