Beispiel #1
0
void kruskal()
{
	int i, e;
	pq_init();
	find_init();

	//1. 모든 정점을 pq삽입한다.
	// 우선순위 큐가 가중치가 가장 낮은 것이 루트가 되도록 정렬
	for (i = 0; i < E; i++)
		pq_insert(i);

	while (!pq_empty())
	{
		e = pq_remove();
		//2. 분리집합인지 확인한다.
		//   edge[e]의 양쪽 정점이 같은 집합에 속해 있는지 확인 한 후
		//   같은 집합이 아니면 최소 신장 트리에 추가
		//   집합을 합친다.
		if (find_set(edge[e].v1, edge[e].v2))
		{
			printf("%c %c\n", int2name(edge[e].v1), int2name(edge[e].v2));
			cost += edge[e].weight;
		}
	}
	printf("최소비용 : %d\n", cost);
}
Beispiel #2
0
void print_adjlist(head net[], int V)
    {          /* print network */
    int i;
    node *t;
    for (i = 0; i < V; i++)
	{
	printf("\n%c:%d | ", int2name(i), net[i].count);
	for (t = net[i].next; t != NULL; t = t->next)
	    printf("-> %c ", int2name(t->vertex));
	}
    }
Beispiel #3
0
void print_adjlist(node *a[], int V)
    {
    int i;
    node *t;
    for (i = 0; i < V; i++)
	{
	printf("\n%c : ", int2name(i));
	for (t = a[i]; t != NULL; t = t->next)
	    printf("-> %c ", int2name(t->vertex));
	}
    }
Beispiel #4
0
void topsort(head net[], int V)
    {         /* function for topological sorting */
    int i, j, k;
    node *ptr;

    init_stack();

    set_count_indegree(net, V);

    for (i = 0; i < V; i++)
	if (!net[i].count) /* search start position of topological sort */
	    push(i);
    for (i = 0; i < V; i++)
	{
	 /* if i < V, network has cycle, case of acyclic network
	    loop must be exhausted */
	if (stack_empty())
	    {
	    printf("\nNetwork has a cycle. Sort Terminated ! ");
	    exit(1);
	    }
	else
	    {
	    j = pop();
	    printf("%c, ", int2name(j));
	    for (ptr = net[j].next; ptr; ptr = ptr->next)
		{
		k = ptr->vertex;
		net[k].count--;
		if (!net[k].count)
		    push(k);
		}
	    }
	}
    }
Beispiel #5
0
int count_components(int a[][MAX_VERTEX], int V)
    {
    int count = 0;
    int i, j;
    init_stack();
    for (i = 0; i < V; i++) check[i] = 0;
    for (i = 0; i < V; i++)
	{
	if (check[i] == 0)
	    {
	    printf("\nConnected Component %d : ", ++count);
	    push(i);
	    check[i] = 1;
	    while (!stack_empty())
		{
		i = pop();
		printf("%c ", int2name(i));
		for (j = 0; j < V; j++)
		    if (a[i][j] != 0)
			if (check[j] == 0)
			    {
			    push(j);
			    check[j] = 1;
			    }
		}
	    }
	}
    printf("\nTotally this graph has %d connected components.", count);
    return count;
    }
Beispiel #6
0
void DFS_directed(node *a[], int V)
    {      /* Get Transitive Closure by Depth First Search */
    int i, j;
    node *t;
    init_stack();
    for (i = 0; i < V; i++)
	{
	for (j = 0; j < V; j++) check[j] = 0;   /* clear */

	push(i);
	check[i] = 1;
	printf("\n %c : ", int2name(i));
	while (!stack_empty())
	    {
	    j = pop();
	    visit(j);
	    for (t = a[j]; t != NULL; t = t->next)
		if (check[t->vertex] == 0)
		    {
		    push(t->vertex);
		    check[t->vertex] = 1;
		    }
	    }
	}
    }
Beispiel #7
0
void print_adjmatrix(int a[][MAX_VERTEX], int V)
    {
    int i, j;

    printf("\n   ");
    for (i = 0; i < V; i++)
	printf("%3c", int2name(i));

    printf("\n");
    for (i = 0; i < V; i++)
	{
	printf("%3c", int2name(i));
	for (j = 0; j < V; j++)
	    printf("%3d", a[i][j]);
	printf("\n");
	}
    }
Beispiel #8
0
void
pr_field(const char *pre, struct field *f, const char *sep)
{
	struct field_pc *pc;
	u_int flags;
	int i, n;
	char *p;

	if (sep)
		printf("%s.%s%s", pre, f->name, sep);

	switch (f->format) {
	case FMT_UINT:
		printf("%u", *((u_int *) f->valp));
		break;
	case FMT_INT:
		printf("%d", *((int *) f->valp));
		break;
	case FMT_BOOL:
		printf("%s", *((u_int *) f->valp)? "on" : "off");
		break;
	case FMT_PC:
		pc = f->valp;
		i = pc->max - pc->min;
		n = pc->cur - pc->min;
		printf("%u.%02u%%", n * 100 / i, ((n * 100) % i) * 100 / i);
		break;
	case FMT_KBDTYPE:
		p = int2name(*((u_int *) f->valp), 1,
			     kbtype_tab, TABLEN(kbtype_tab));
		printf("%s", p);
		break;
	case FMT_MSTYPE:
		p = int2name(*((u_int *) f->valp), 1,
			     mstype_tab, TABLEN(mstype_tab));
		printf("%s", p);
		break;
	case FMT_DPYTYPE:
		p = int2name(*((u_int *) f->valp), 1,
			     dpytype_tab, TABLEN(dpytype_tab));
		printf("%s", p);
		break;
	case FMT_KBDENC:
		p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
			     kbdenc_tab, TABLEN(kbdenc_tab));
		printf("%s", p);

		flags = KB_VARIANT(*((u_int *) f->valp));
		for (i = 0; i < 32; i++) {
			if (!(flags & (1 << i)))
				continue;
			p = int2name(flags & (1 << i), 1,
				     kbdvar_tab, TABLEN(kbdvar_tab));
			printf(".%s", p);
		}
		break;
	case FMT_KBMAP:
		print_kmap((struct wskbd_map_data *) f->valp);
		break;
	case FMT_SCALE:
		printf("%d,%d,%d,%d,%d,%d,%d", wmcoords.minx, wmcoords.maxx,
		    wmcoords.miny, wmcoords.maxy, wmcoords.swapxy,
		    wmcoords.resx, wmcoords.resy);
		break;
	case FMT_EMUL:
		print_emul((struct wsdisplay_emultype *) f->valp);
		break;
	case FMT_SCREEN:
		print_screen((struct wsdisplay_screentype *) f->valp);
		break;
	case FMT_STRING:
		printf("%s", (const char *)f->valp);
		break;
	default:
		errx(1, "internal error: pr_field: no format %d", f->format);
		break;
	}

	printf("\n");
}
Beispiel #9
0
void visit(int vertex)
    {
    printf("%c ", int2name(vertex));
    }