Beispiel #1
0
	void main()

	{       int i,j,n;
		cout<<"请输入有向图节点,以-1 -1结束,和节点个数n:"<<endl;
		cin>>i>>j;
		while(i!=-1 && j!=-1)
		{
		 if(i<0||j<0)
		 {
		cout<<"输入错误"<<endl; 
		exit(-2);
		 }
		 arc[i][j]=1;
		 cin>>i>>j;

		}
		cin>>n;
		if(n<1||n>Maxnode)
		{
		 cout<<"错误,节点数必须是在1到"<<Maxnode<<"之间的数!"<<endl;
		 exit(-2);
		}
		cout<<"对应的邻接矩阵如下:"<<endl;
		showMG(n);
		topsort(n);
	}
Beispiel #2
0
// Test Topological Sort with BFS (queue)
// Version for Adjancency List representation
int main(int argc, char** argv) {
  Graph* G;
  FILE *fid;

  if (argc != 2) {
    cout << "Usage: grdfsm <file>\n";
    exit(-1);
  }

  if ((fid = fopen(argv[1], "rt")) == NULL) {
    cout << "Unable to open file |" << argv[1] << "|\n";
    exit(-1);
  }

  G = createGraph<Graphl>(fid);
  if (G == NULL) {
    cout << "Unable to create graph\n";
    exit(-1);
  }

  Queue<int>* Q = new AQueue<int>(G->n());

  topsort(G, Q);
  cout << endl;
  return 0;
}
Beispiel #3
0
void topsort(int dp,int cnt)
{
     if(dp>=cnt)
     {
                for(int i=0;i<dp;i++)
                printf("%c",solution[i]+'A');
                printf("\n");
     }
     for(int i=0;i<26;i++)
     {
             if(vi[i]&&number[i]==0)
             {
                             solution[dp++]=i;
                             vi[i]=0;
                             for(int j=1;j<=g1[i][0];j++)
                             {
                                     number[g1[i][j]]--;
                             }
                             topsort(dp,cnt);
                             dp--;
                             vi[i]=1;
                             for(int j=1;j<=g1[i][0];j++)
                             {
                                     number[g1[i][j]]++;
                             }
                             number[i]=0;
             }
     }
}
Beispiel #4
0
void main(int argc, char *argv[])
    {
    int V, E;

    if (argc < 2)
	fp = stdin;
    else
	if ((fp = fopen(argv[1], "rt")) == NULL)
	    {
	    printf("\n That file does not exist!");
	    exit(1);
	    }

    input_adjlist(network, &V, &E);
    printf("\n\nAdjacency List representaion for graph");
    print_adjlist(network, V);
    printf("\n\nSet count to indegree");
    set_count_indegree(network, V);
    print_adjlist(network, V);
    printf("\n\nTopological order\n");
    topsort(network, V);
    getch();
    printf("\n\nSet count to outdegree");
    set_count_outdegree(network, V);
    print_adjlist(network, V);
    printf("\n\nReverse Topological order\n");
    revtopsort(network, V);
    }
Beispiel #5
0
int main(void) {
	int N = -1, M = -1;
	FILE * in, * out;
	struct node ** lists = NULL;
	struct node ** tmp = NULL;

	in = fopen(INPUT_FILE, "r");
	out = fopen(OUTPUT_FILE, "w");
	readFirstValues(in, &N, &M, out);
	allocMemForLists(&lists, N);
	tmp = getValues(N, M, lists, in, out);
	if (topsort(lists, N, out)) {
		remove_lists(lists, N);
		free(tmp);
		free(lists);
		ERROR(out, ERROR_CYCLE);
	}
	remove_lists(lists, N);
	free(tmp);
	free(lists);
	fclose(in);
	fclose(out);

	return(0);
}
Beispiel #6
0
int main(void)
{
	struct graph g;
	int k;

	graph_creat(&g, "graph.txt", 1); // create DAG
	graph_print(&g);

	topsort(&g);
	printf("topological sort: ");
	for (k = 0; k < g.nnode; k++)
		printf("%d-> ", sort[k]);
	printf("\n");

	return 0;
}
Beispiel #7
0
main()
{
	graph g;
	int out[MAXV];
	int i;

	read_graph(&g,TRUE);
	print_graph(&g);

	topsort(&g,out);

	for (i=1; i<=g.nvertices; i++)
		printf(" %d",out[i]);
	printf("\n");

}
Beispiel #8
0
int main(int argc, char *argv[])
{
    srand(time(NULL));
    int arr[100] = { 3, 1, 4, 7, 10, 11 };
    int n = 6;
    if (argc == 1)
    {
        printf("Invalid num args\nCommand line input was expected\nRunning with default number\n");
        putchar('\n');
        int i;
        for(i=0; i<n; ++i)
            printf("%d ", arr[i]);
        putchar('\n');
    }
    else if (argc == 2)
    {
        n = atoi(argv[1]);
        printf("In : ");
        gen(arr, n);
        putchar('\n');;
    }
    else
    {
        n = atoi(argv[1]);
        int i;
        printf("In : ");
        for(i=0; i<n; ++i)
            printf("%d ", arr[i] = atoi(argv[i + 2]));
        putchar('\n');;
    }
    init();
    int x = srt(arr, n);
    if (x == -INF)
    {
        printf("No repetitions\n");
    }
    else
    {
        printf("Repition :");
        printf("%d ", x);
        putchar('\n');
    }
    printf("Topsort: ");
    topsort();
    putchar('\n');
    return 0;
}
Beispiel #9
0
void mig_cuts_paged::enumerate()
{
  reference_timer t( &_enumeration_time );

  /* topsort */
  std::vector<unsigned> topsort( num_vertices( _mig ) );
  boost::topological_sort( _mig, topsort.begin() );

  /* loop */
  _top_index = 0u;
  for ( auto n : topsort )
  {
    if ( out_degree( n, _mig ) == 0u )
    {
      /* constant */
      if ( n == 0u )
      {
        data.assign_empty( 0u, {0u, 0u} );
        cones.assign_empty( 0u );
      }
      /* PI */
      else
      {
        data.assign_singleton( n, n, {0u, 1u} );
        cones.assign_singleton( n, n );
      }
    }
    else
    {
      data.append_begin( n );
      cones.append_begin( n );

      /* get children */
      auto it = adjacent_vertices( n, _mig ).first;
      const auto n1 = *it++;
      const auto n2 = *it++;
      const auto n3 = *it++;

      enumerate_node_with_bitsets( n, n1, n2, n3 );

      data.append_singleton( n, n, {0u, 1u} );
      cones.append_singleton( n, n );
    }

    _top_index++;
  }
}
Beispiel #10
0
int main(int argc, const char *argv[])
{
	char * 	v[N] = {"V0", "V1", "V2", "V3", "V4", "V5", "V6", "V7", "V8"};
    int edge[N][N]={   //v0 v1 v2 v3 v4 v5 v6 v7 v8
                 /*v0*/ {0, 1, 1, 0, 0, 0, 0, 0, 0},
                 /*v1*/ {1, 0, 0, 1, 0, 1, 0, 0, 0},
                 /*v2*/ {1, 0, 0, 0, 0, 0, 1, 1, 0},
                 /*v3*/ {0, 1, 0, 0, 1, 0, 0, 0, 0},
                 /*v4*/ {0, 0, 0, 1, 0, 1, 0, 0, 1},
                 /*v5*/ {0, 1, 0, 0, 1, 0, 0, 0, 0},
                 /*v6*/ {0, 0, 1, 0, 0, 0, 0, 1, 0},
                 /*v7*/ {0, 0, 1, 0, 0, 0, 1, 0, 0},
                 /*v8*/ {0, 0, 0, 0, 1, 0, 0, 0, 0}
    };
	mgraph * mg;

	mg = mgraph_create(N, v, edge);

	//int visited[N] = {0};
	//DFS(mg, 0, visited);
	_DFS(mg, 0);
	puts("");

	_BFS(mg, 0);

	if (topsort(mg)) {
		printf("not cycle\n");
	}
	else {
		printf("cycle\n");
	}

	//int (*p)[N];
//	p = edge;
#if 0
	
	int u = firstadj(mg, 7);//u=1
	printf("%d %s \n", u, v[u]);//1 V1

	u = nextadj(mg, 7, u);//u=2
	printf("%d %s \n", u, v[u]);//1 V1

#endif

	return 0;
}
Beispiel #11
0
std::vector<std::pair<unsigned, unsigned>> compute_level_ranges( const mig_graph& mig, unsigned& max_level )
{
  auto sa_settings = std::make_shared<properties>();
  auto sa_statistics = std::make_shared<properties>();

  auto output_levels = simulate_mig( mig, mig_depth_simulator(), sa_settings, sa_statistics );

  max_level = 0u;
  const auto& info = mig_info( mig );
  for ( const auto& o : info.outputs )
  {
    max_level = std::max( max_level, output_levels.at( o.first ) );
  }

  auto levels = sa_statistics->get<std::map<mig_node, unsigned>>( "node_values" );
  std::vector<std::pair<unsigned, unsigned>> level_ranges( num_vertices( mig ) );

  std::vector<mig_node> topsort( boost::num_vertices( mig ) );
  boost::topological_sort( mig, topsort.begin() );

  auto ingoing = precompute_ingoing_edges( mig );

  for ( const auto& v : topsort )
  {
    auto it = ingoing.find( v );

    /* no ingoing edges (outputs) */
    if ( it == ingoing.end() )
    {
      level_ranges[v] = {levels[v], max_level};
      levels[v] = max_level;
      continue;
    }

    const auto min_edge = *boost::min_element( it->second, [&]( const mig_edge& e1, const mig_edge& e2 ) {
        return levels.at( boost::source( e1, mig ) ) < levels.at( boost::source( e2, mig ) );
      } );
    auto to_l = levels.at( boost::source( min_edge, mig ) ) - 1u;
    level_ranges[v] = {levels[v], to_l};
    levels[v] = to_l;
  }

  return level_ranges;
}
Beispiel #12
0
	int main() {
		init();
		build_graph();
		topsort();
		sgt::build(0, axis - 1);
		int res = inf;
		for ( int i = n_stuff - 1; i >= 0; i -- ) {
			int t = cmd[i].des;
			if ( cmd[i].on ) {
				int l = stuff[t].l, r = stuff[t].r - 1;
				if ( l <= r ) {
					int tmp = sgt::ask_min(l, r);
					if ( tmp < val[t] )
						res = i;
				}
			}
			if ( stuff[t].r > stuff[t].l )
				sgt::cover(stuff[t].l, stuff[t].r - 1, val[t]);
		}
		return res;
	}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        int top=-1;
        memset(in,0,sizeof(in));
        memset(vert,0,sizeof(vert));
        for(int i=1;i<=n;i++)
        {
            int m,x;
            scanf("%d%d",&tim[i],&m);
            in[i]=m;
            for(int j=0;j<m;j++)
            {
                scanf("%d",&x);
                Add_Edge(x,i,top);
            }
        }
        printf("%d\n",topsort());
    }
    return 0;
}
Beispiel #14
0
int main(int argc, const char *argv[])
{
	char * 	v[N] = {"V0", "V1", "V2", "V3", "V4", "V5"};
	int edge[N][N]={
	  /*		 v0 v1 v2 v3 v4 v5						*/	
	  /*v0*/	{0, 1, 0, 1, 1, 0},
	  /*v1*/	{0, 0, 0, 0, 0, 1},
	  /*v2*/	{0, 1, 0, 0, 0, 1},
	  /*v3*/	{0, 0, 0, 0, 0, 0},
	  /*v4*/	{0, 0, 0, 1, 0, 1},
	  /*v5*/	{0, 0, 0, 0, 0, 0}};

	mgraph * mg;
	mg = mgraph_create(N, v, edge);

	if (topsort(mg)) {
		printf("not cycle\n");
	}
	else {
		printf("cycle\n");
	}

	return 0;
}
Beispiel #15
0
int main()
{
    while(1==scanf("%d",&n))
    {
                            memset(g,0,sizeof(g));
                            memset(vi,0,sizeof(vi));
                            memset(gg,-1,sizeof(gg));
                            memset(number,0,sizeof(number));
                            memset(g1,0,sizeof(g1));
                            frame fp[41];
                            scanf("%d",&m);
                            for(int i=1;i<=n;i++)
                            scanf("%s",mp[i]+1);
                            for(int i=1;i<=n;i++)
                            for(int j=1;j<=m;j++)
                            {
                                    if(mp[i][j]=='.')
                                                     continue;
                                    int a=mp[i][j]-'A';
                                    gg[i][j]=a;
                                    vi[a]=1;
                                    if(fp[a].x1>i)fp[a].x1=i;
                                    if(fp[a].y1>j)fp[a].y1=j;
                                    if(fp[a].x2<i)fp[a].x2=i;
                                    if(fp[a].y2<j)fp[a].y2=j;
                            }
                            int cnt=build(fp);
                  /*          for(int i=0;i<=4;i++)
                            printf("%d %d %d %d\n",fp[i].x1,fp[i].y1,fp[i].x2,4fp[i].y2);
                            for(int i=0;i<26;i++)
                            if(number[i])
                            printf("%d ",i); */
                            topsort(0,cnt); 
    }
    return 0;
}
Beispiel #16
0
abc::Gia_Man_t* cirkit_to_gia( const aig_graph& aig )
{
  const auto& info = aig_info( aig );

  const unsigned _num_inputs = info.inputs.size();
  const unsigned _num_outputs = info.outputs.size();
  const unsigned _num_latches = info.cis.size();
  const unsigned _num_vertices = num_vertices( aig ) - 1u;
  const unsigned _num_gates = _num_vertices - _num_latches - _num_inputs;

  assert( _num_vertices == _num_inputs + _num_latches + _num_gates );

  /* allocate an empty aig in abc */
  abc::Gia_Man_t * gia = abc::Gia_ManStart( _num_vertices + _num_latches + _num_outputs + 1u );
  gia->nConstrs = 0;
  gia->pName = strcpy( (char*)malloc( sizeof( char ) * ( info.model_name.size() + 1u ) ), info.model_name.c_str() );

  std::vector< int > node_to_obj( boost::num_vertices( aig ) );
  node_to_obj[0] = 0;

  /* inputs */
  assert( !gia->vNamesIn );
  gia->vNamesIn = abc::Vec_PtrStart( info.inputs.size() );
  for ( const auto& input : index( info.inputs ) )
  {
    const int obj = abc::Gia_ManAppendCi( gia );
    node_to_obj[input.value] = abc::Abc_Lit2Var( obj );
    const auto name = info.node_names.at( input.value );
    abc::Vec_PtrSetEntry( gia->vNamesIn, input.index, strcpy( (char*)malloc( sizeof( char ) * ( name.size() + 1u ) ), name.c_str() ) );
  }

  /* latches */
  assert( info.cis.size() == info.cos.size() );
  assert( _num_latches == 0u );

  /* and gates */
  std::vector<unsigned> topsort( boost::num_vertices( aig ) );
  boost::topological_sort( aig, topsort.begin() );

  for ( const auto& node : topsort )
  {
    if ( !boost::out_degree( node, aig ) ) { continue; }

    const auto children = get_children( aig, node );
    assert( children.size() == 2u );

    const int obj = Gia_ManAppendAnd2_Simplified( gia,
                                                  abc::Abc_Var2Lit( node_to_obj[children[0].node], children[0].complemented ),
                                                  abc::Abc_Var2Lit( node_to_obj[children[1].node], children[1].complemented ) );

    node_to_obj[node] = abc::Abc_Lit2Var( obj );
  }

  /* outputs */
  assert( !gia->vNamesOut );
  gia->vNamesOut = abc::Vec_PtrStart( info.outputs.size() );
  for ( const auto& output : index( info.outputs ) )
  {
    abc::Gia_ManAppendCo( gia, abc::Abc_Var2Lit( node_to_obj[output.value.first.node], output.value.first.complemented ) );
    const auto name = output.value.second;
    abc::Vec_PtrSetEntry( gia->vNamesOut, output.index, strcpy( (char*)malloc( sizeof( char ) * ( name.size() + 1u ) ), name.c_str() ) );
  }

  return gia;
}
Beispiel #17
0
  void map()
  {
    auto names = boost::get( boost::vertex_name, lut );
    auto tts = boost::get( boost::vertex_lut, lut );
    auto types = boost::get( boost::vertex_lut_type, lut );

    std::vector<lut_vertex_t> topsort( num_vertices( lut ) );
    boost::topological_sort( lut, topsort.begin() );
    for ( auto v : topsort )
    {
      switch ( types[v] )
      {
      case lut_type_t::internal:
        {
          std::vector<xmg_function> pi_mapping;
          for ( const auto& child : boost::make_iterator_range( boost::adjacent_vertices( v, lut ) ) )
          {
            pi_mapping.push_back( node_to_function[child] );
          }

          /* some special cases */
          if ( boost::out_degree( v, lut ) <= 1u )
          {
            if ( tts[v] == "2" )
            {
              node_to_function[v] = pi_mapping.front();
              break;
            }
            else if ( tts[v] == "1" )
            {
              node_to_function[v] = !pi_mapping.front();
              break;
            }

            std::cout << "[e] special case for " << tts[v] << " with " << boost::out_degree( v, lut ) << " input missing" << std::endl;
            assert( false );
          }

          const auto it = optimal_xmgs.find( tts[v] );
          assert ( it != optimal_xmgs.end() );

          const auto opt_xmg = it->second;
          if ( opt_xmg.inputs().size() != boost::out_degree( v, lut ) )
          {
            boost::dynamic_bitset<> phase;
            std::vector<unsigned> perm;
            tt orig( convert_hex2bin( tts[v] ) );
            auto npn = npn_canonization_lucky( orig, phase, perm );

            std::cout << "[e] problem when mapping LUT " << v << " with TT " << tts[v] << " to XMG" << std::endl;
            std::cout << "[e] LUT has " << boost::out_degree( v, lut ) << " inputs" << std::endl;
            std::cout << "[e] XMG has " << opt_xmg.inputs().size() << " inputs" << std::endl;
            std::cout << "[e] TT: " << orig << ", NPN: " << npn << std::endl;
            assert( false );
          }
          const auto outputs = xmg_rewrite_top_down_inplace( xmg, opt_xmg, rewrite_default_maj, rewrite_default_xor, pi_mapping );

          if ( outputs.size() != 1u )
          {
            std::cout << "[e] expected: 1, got: " << outputs.size() << std::endl;
            assert( false );
          }

          node_to_function[v] = outputs.front();
        } break;

      case lut_type_t::po:
        {
          xmg.create_po( node_to_function[*boost::adjacent_vertices( v, lut ).first], names[v] );
        } break;

      default:
        break;
      }
    }
  }