# 纸上谈兵: 图 (graph)

(这个问题在许多奥数教材中称为"一笔画"问题)

### 图的定义

(七桥问题中的图是无向的。城市中的公交线路可以是无向的，比如存在单向环线)

### 图的实现

 a 1 2 3 1 0 1 1 2 0 0 0 3 0 1 0

/* By Vamei */
#include <stdio.h>
#include <stdlib.h>

#define NUM_V 5

typedef struct node *position;

/* node */
struct node {
int element;
position next;
};

/*
* operations (stereotype)
*/
void insert_edge(position, int, int);
void print_graph(position graph, int nv);

/* for testing purpose */
void main()
{
struct node graph[NUM_V];
int i;

// initialize the vertices
for(i=1; i<NUM_V; i++) {
(graph+i)->element = i;
(graph+i)->next    = NULL;
}

// insert edges
insert_edge(graph,1,2);
insert_edge(graph,1,4);
insert_edge(graph,3,2);
insert_edge(graph,4,2);
insert_edge(graph,4,3);

print_graph(graph,NUM_V);
}

/* print the graph */
void print_graph(position graph, int nv) {
int i;
position p;
for(i=1; i<nv; i++) {
p = (graph + i)->next;
printf("From %3d: ", i);
while(p != NULL) {
printf("%d->%d; ", i, p->element);
p = p->next;
}
printf("\n");
}
}

/*
* insert an edge
*/
void insert_edge(position graph,int from, int to)
{
position np;

np = graph + from;

}

From   1: 1->4; 1->2;
From   2:
From   3: 3->2;
From   4: 4->3; 4->2;

### 总结

posted @ 2013-08-01 14:37  Vamei  阅读(...)  评论(...编辑  收藏