Пример #1
0
int adjacent(Graph g, Jval v1, Jval v2,
				int (*cmp)(Jval, Jval))
{
	JRB found = jrb_find_gen(g, v1, cmp);

	if(found == NULL){
		return 0;
	}else{
		found = jrb_find_gen((JRB) jval_v(found->val), v2, cmp);
		if(found == NULL) return 0;
		else return 1;
	}

}
Пример #2
0
int solve(FILE *f) {
	Graph g;
	JRB stationList;

	String startName, stopName;
	JRB startCode, stopCode, node;
	int n;
	Jval output[SIZE];

	g = createGraph();
	stationList = make_jrb();

	if(readFile(f, g, stationList) == -1) {
		return 1;
	}

	printf("Please enter the start station name:\n");
	fscanf(stdin, "%[^\n]", startName.content);
	while(getchar() != '\n');

	printf("Please enter the stop station name:\n");
	fscanf(stdin, "%[^\n]", stopName.content);
	while(getchar() != '\n');

	if((startCode = jrb_find_gen(stationList, new_jval_v(&startName), stringCompare)) == NULL) {
		return 1;
	}

	if((stopCode = jrb_find_gen(stationList, new_jval_v(&stopName), stringCompare)) == NULL) {
		return 1;
	}

	printf("Movement between station \"%s\" to station \"%s\"\n", startName.content, stopName.content);
	n = UShortestPath(g, startCode->val, stopCode->val, cloneNode, stringCompare, myPrint);

	if(n == 0) {
		printf("Cannot move from station \"%s\" to station \"%s\"\n", startName.content, stopName.content);
	} else {
		printf("Path length between station \"%s\" to station \"%s\": %d\n", startName.content, stopName.content, n);
	}


	freeGraph(g);
	jrb_free_tree(stationList);
	return 0;
}
Пример #3
0
JRB getAdjVertices(Graph g, Jval v1,
							int (*cmp)(Jval, Jval))
{
	JRB tmp, found;
	found = jrb_find_gen(g, v1, cmp);

	if(found == NULL) return NULL;
	else return (JRB) jval_v(found->val);
}
Пример #4
0
int UShortestPath(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {

	Dllist node, queue, stackVisit;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return 0;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			jrb_insert_gen(visited, temp, temp, compare);
			dll_prepend(stackVisit, temp);
			
			if(compare(temp, stop) == 0) {
				return solution(graph, start, stop, stackVisit, cloneFunc, compare, reportFunc);
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return -1;
}
Пример #5
0
void DFS(Graph graph, Jval start, Jval stop, 
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	Dllist node, stack;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return;
	}

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				free(output);
				return;
			}

			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_prepend(stack, output[i]);
				}
			}
		}
	}
}
Пример #6
0
int insertEdge(Graph g, Jval v1, Jval v2,
				int (*cmp)(Jval, Jval))
{
	Graph found;
	Jval j_adj_list;
	Jval j = new_jval_v(NULL);
	found = jrb_find_gen(g, v1, cmp);

	if(found == NULL){
		/* if not found
		 create a new adjacent list which v2 is added to
		 then insert the adjacent list to the tree
		 with the key is v1
		*/

		 // create a new adj_list
		JRB adj_list = make_jrb();
		jrb_insert_gen(adj_list, v2, j, cmp);
		j_adj_list = new_jval_v(adj_list);

		// add adjacent list to the tree
		jrb_insert_gen(g, v1, j_adj_list, cmp);

		return 0;

	}else{
		/*
		if found
		check if v2 is in the adj_list of v1
				if not, add v2 to adj_list of v1
				else return status code 1
		*/

		JRB adj_list = (JRB) jval_v(found->val);
		found = jrb_find_gen(adj_list, v2, cmp);
		if(found == NULL){
			jrb_insert_gen(adj_list, v2, j, cmp);
			return 0;
		}else return 1;
	}
}
Пример #7
0
int BFStraverse(Graph graph, Jval start,
	Jval (*cloneFunc)(Jval), int (*compare)(Jval, Jval), void (*reportFunc)(Jval)) {
	
	Dllist node, queue;
	JRB visited;
	Jval *output;
	Jval temp, tmp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, start);

	if((output = myMalloc(sizeof(Jval), 100)) == NULL) {
		return counter;
	}

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = cloneFunc(node->val);
		dll_delete_node(node);

		if(jrb_find_gen(visited, temp, compare) == NULL) {
			counter++;
			reportFunc(temp);
			jrb_insert_gen(visited, temp, temp, compare);
			
			n = getAdjacentVertices(graph, temp, output, compare);
			for(i = 0; i < n; i++) {
				if(jrb_find_gen(visited, output[i], compare) == NULL) {
					dll_append(queue, output[i]);
				}
			}
		}
	}

	return counter;
}
Пример #8
0
int getAdjVerticesI(Graph g, Jval v, int* output,
						int (*cmp)(Jval, Jval))
{
	// find note v
	JRB found = jrb_find_gen(g, v, cmp);
	JRB adj_list;
	JRB tmp;
	int count = 0;

	if(found == NULL){
		printf("ERROR: Node %d does not exist\n", jval_i(v));
		return 0;
	}else{
		adj_list = (JRB) jval_v(found->val);
		jrb_traverse(tmp, adj_list){
			output[count] = jval_i(tmp->key);
			count++;
		}
		return count;
	}