Example #1
0
int main (void){
	read();
	print_list_graph();
	int *Ts = topo_sort();
	print_int_array(Ts,0,n-1);
	return 0;
}
Example #2
0
struct copy_graph_node * permute_copies(
    struct copy_graph_node **copy_graph_head, struct add_list * add_list)
{
    int count;
    struct copy_graph_node * topo_list_head;
    struct copy_graph_node ** node_array;

    if(do_stats) {
        timing(TIMING_START);
    }
    node_array = build_edges(*copy_graph_head,&count);
    if(do_stats) {
        rprintf(FINFO, "Build graph: %s\n", timing(TIMING_END));
    }
    if(do_stats) {
        timing(TIMING_START);
    }
    topo_list_head = topo_sort(node_array,count, add_list);
    if(do_stats) {
        rprintf(FINFO, "Topological sort: %s\n", timing(TIMING_END));
    }
    update_extra_memory(-(count * sizeof(struct copy_graph_node * )));
    if(do_stats) {
        rprintf(FINFO, "Extra memory: %d Extra bytes sent: %d\n",
                max_extra_memory,extra_bytes);
        rprintf(FINFO, "Broken copies without delta comp: %d\n",
                broken_copies);
        rprintf(FINFO, "Cycles broken: %d Bytes trimmed: %d\n",
                cycles_broken, bytes_trimmed);
    }

    free(node_array);
    return topo_list_head;
}
    void randomize()
    {
	Timer t;
	for(int i = 1;i < 20;i++)
	{
	   cout << "\nFor operation : " << i;
	   nodes = i;

	   //general initializations
	   visited = new int [nodes];
	   *edges = new int [nodes];
	   for(int i = 0;i < nodes;i++) edges[i] = new int [nodes];
	   for(int row = 0;row < nodes;row++)
	       for(int col = 0;col < nodes;col++)
	       {   visited[row] = 0;
		   edges[row][col] = -1;
	       }
	   //-----------------------

	   for(int k = 0;k < nodes;k++)
	      for(int l = 0;l < nodes;l++)
	      {
		  if(edges[k][l] != -1) continue;
		  edges[k][l] = rand() % 2;
		  edges[l][k] = 0;
	      }

	  t.start();topo_sort();t.stop();stck.clear();
	  cout << " time taken is : " << t.time() << " seconds";
	}
    }
int main()
{

    int mat[101][101]={0};
    int i,j,k;
    int V,E;
    int r,c;
    while((scanf("%d%d",&V,&E)==2) && (V || E))
{
    for(i=0;i<E;i++)
    {
	k=scanf("%d%d",&r,&c);
	mat[r-1][c-1] =1;
    }
#if 0
    for(i=0;i<V;i++)
    {
	for(j=0;j<V;j++)
	    printf("%d ",mat[i][j]);
	printf("\n");
    }
#endif
    topo_sort(mat,V);
    for(i=0;i<V;i++)
     for(j=0;j<V;j++)
	mat[i][j] = 0;
    printf("\n");
}
    return(0);
}
Example #5
0
void RectCluster::sequential_partition()
{
	SequentialWSPD seq_wspd(curve1_, curve2_, 1 / eps_);
	std::vector<IndexSegment> segs1 = seq_wspd.segs1();
	std::vector<IndexSegment> segs2 = seq_wspd.segs2();
	
	gen_rect_from_pair(segs1, segs2);

	build_rect_graph();
	topo_sort();
}
Example #6
0
/*
 * Given a graph, do a recursive topological sort into the given array.  This is
 * really just a depth first search, so that the deepest nodes appear first.
 * hijack the 'zv_visited' marker to avoid visiting the same vertex twice.
 */
static int
topo_sort(libzfs_handle_t *hdl, boolean_t allowrecursion, char **result,
    size_t *idx, zfs_vertex_t *zgv)
{
	int i;

	if (zgv->zv_visited == VISIT_SORT_PRE && !allowrecursion) {
		/*
		 * If we've already seen this vertex as part of our depth-first
		 * search, then we have a cyclic dependency, and we must return
		 * an error.
		 */
		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
		    "recursive dependency at '%s'"),
		    zgv->zv_dataset);
		return (zfs_error(hdl, EZFS_RECURSIVE,
		    dgettext(TEXT_DOMAIN,
		    "cannot determine dependent datasets")));
	} else if (zgv->zv_visited >= VISIT_SORT_PRE) {
		/*
		 * If we've already processed this as part of the topological
		 * sort, then don't bother doing so again.
		 */
		return (0);
	}

	zgv->zv_visited = VISIT_SORT_PRE;

	/* avoid doing a search if we don't have to */
	zfs_vertex_sort_edges(zgv);
	for (i = 0; i < zgv->zv_edgecount; i++) {
		if (topo_sort(hdl, allowrecursion, result, idx,
		    zgv->zv_edges[i]->ze_dest) != 0)
			return (-1);
	}

	/* we may have visited this in the course of the above */
	if (zgv->zv_visited == VISIT_SORT_POST)
		return (0);

	if ((result[*idx] = zfs_alloc(hdl,
	    strlen(zgv->zv_dataset) + 1)) == NULL)
		return (-1);

	(void) strcpy(result[*idx], zgv->zv_dataset);
	*idx += 1;
	zgv->zv_visited = VISIT_SORT_POST;
	return (0);
}
Example #7
0
void sssp_dag (void) {
    topo_sort();
    memset(dis, 0xff, sizeof(dis));
    dis[s] = 0;
    for (int i = cnt_topo_list; i >= 1; --i) {
        int node = topo_list[i];
        for (int j = 1; j <= n; ++j) {
            if (map[node][j]) {
                if (dis[node] >= 0) {
                    if (dis[node] + map[node][j] > dis[j]) {
                        dis[j] = dis[node] + map[node][j];
                    }
                }
            }
        }
    }
}
Example #8
0
void RectCluster::partition()
{
  VLOG(6) << "Grid side length:   " << len_cell_;
	
	// random shift
	unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
	std::default_random_engine generator(seed);
  std::uniform_real_distribution<double> distribution(0.0, (double) len_cell_);
	double rand_shift_x = distribution(generator);
	double rand_shift_y = distribution(generator);
	Vector_2 vec(rand_shift_x, rand_shift_y);
	LOG(INFO) << "Random shift: " << vec;
	
	// Build grid
	grid_ = new Grid(curve1_, curve2_, lb_, ub_, len_cell_);
	grid_->init(vec);
	
	for (auto it = grid_->begin(); it != grid_->end(); it++)
	{
		// quad-tree from containing points from curve1
		QuadTree* curr_qt = it->second.first;
		// check if the node contains points from curve 1
		if (curr_qt->is_empty())
		{
			continue;
		}
		// find the neighboring nodes that contain points from curve 2
		vector<QuadTree*> nbrs = grid_->neighbors(it->first, 1);
		//VLOG(7) << it->second.first->to_string() << " ---------- ";
		for (auto& qt : nbrs)
		{
			WSPD wspd(curr_qt, qt, 1 / eps_, lb_ / max(curve1_.size(), curve2_.size()));
			for (auto it = wspd.begin(); it != wspd.end(); it++)
			{
				gen_rect(it->first->idx_segments(), it->second->idx_segments());
			}
		}
	}
	
	// after generating all rectangles, build a dependency graph of them and topologically sort them
	build_rect_graph();
	topo_sort();
}
Example #9
0
/*
 * The only public interface for this file.  Do the dirty work of constructing a
 * child list for the given object.  Construct the graph, do the toplogical
 * sort, and then return the array of strings to the caller.
 *
 * The 'allowrecursion' parameter controls behavior when cycles are found.  If
 * it is set, the the cycle is ignored and the results returned as if the cycle
 * did not exist.  If it is not set, then the routine will generate an error if
 * a cycle is found.
 */
int
get_dependents(libzfs_handle_t *hdl, boolean_t allowrecursion,
    const char *dataset, char ***result, size_t *count)
{
	zfs_graph_t *zgp;
	zfs_vertex_t *zvp;

	if ((zgp = construct_graph(hdl, dataset)) == NULL)
		return (-1);

	if ((*result = zfs_alloc(hdl,
	    zgp->zg_nvertex * sizeof (char *))) == NULL) {
		zfs_graph_destroy(zgp);
		return (-1);
	}

	if ((zvp = zfs_graph_lookup(hdl, zgp, dataset, 0)) == NULL) {
		free(*result);
		zfs_graph_destroy(zgp);
		return (-1);
	}

	*count = 0;
	if (topo_sort(hdl, allowrecursion, *result, count, zvp) != 0) {
		free(*result);
		zfs_graph_destroy(zgp);
		return (-1);
	}

	/*
	 * Get rid of the last entry, which is our starting vertex and not
	 * strictly a dependent.
	 */
	assert(*count > 0);
	free((*result)[*count - 1]);
	(*count)--;

	zfs_graph_destroy(zgp);

	return (0);
}
int main()
{

    BORDER b[201]={0};
    char tmp[52]={0};
    int mat[101][101]={0};
    int i,j,k;
    int V,E;
    int r,c;
    int t=1;
    while((scanf("%d",&V)==1))
    {
	for(i=0;i<V;i++)
	{
	    k = scanf("%s",tmp);
	    strcpy(bevarages[i],tmp);
	}
	k = scanf("%d",&E);
	for(i=0;i<E;i++)
	{
	    k = scanf("%s%s",b[i].B1,b[i].B2);
	    for(j=0;j<V;j++)
	    {
		if(strcmp(b[i].B1,bevarages[j])==0)
		{
		    r = j;
		    break;
		};
	    }
	    for(j=0;j<V;j++)
	    {
		if(strcmp(b[i].B2,bevarages[j])==0)
		{
		    c = j;
		    break;
		};
	    }
	    mat[r][c] =1;
	}
#if 0
	for(i=0;i<V;i++)
	{
	    for(j=0;j<V;j++)
		printf("%d ",mat[i][j]);
		//printf("[%s,%s] ",bevarages[i],bevarages[j]);
	    printf("\n");
	}
	for(i=0;i<V;i++)
	    printf("%d  %s\n",i,bevarages[i]);
#endif
	topo_sort(mat,V);
        printf("Case #%d: Dilbert should drink beverages in this order: ",t++);
	for(i=0;i<V-1;i++)
	    printf("%s ",bevarages[topo_order[i]-1]);
	printf("%s.",bevarages[topo_order[i]-1]);
	for(i=0;i<V;i++)
	{
	    for(j=0;j<V;j++)
		mat[i][j] = 0;
	    topo_order[i]=0;
	} 
	getchar();
	printf("\n\n");
       
    }
    return(0);
}