コード例 #1
0
int get_node(uint64_t node_id) {
	if(DEBUG) {
		printf("Start the process of getting node %llu...\n", node_id);
	}
	// printf("*Breakpoint0\n");
	AdjList *curr = graph->adjList;
	if(curr == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	curr = curr->next;
	while(curr != NULL) {
		if(curr->node_id == node_id) {
			if(DEBUG) {
				printf("Node %llu exists!\n", node_id);
				printAdjList();
			}
			return 0;
		}
		curr = curr->next;
	}
	if(DEBUG) {
		printf("Node %llu does not exist!\n", node_id);
		printAdjList();
	}
	return -1;
}
コード例 #2
0
int get_neighbors(uint64_t node_id, AdjListNode **p_neighbors) {
	if(DEBUG) {
		printf("Start the process of getting neighbors of node %llu...\n", node_id);
	}
	// printf("*Breakpoint0\n");
	AdjList *curr = graph->adjList;
	if(curr == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	curr = curr->next;
	while(curr != NULL) {
		if(curr->node_id == node_id) {
			if(DEBUG) {
				printf("Node %llu exists!\n", node_id);
				printf("Assign head of curr to neighbors!\n");
			}
			*p_neighbors = curr->head;
			// for (AdjListNode *tmp = *p_neighbors; tmp != NULL; tmp = tmp->next) {
			//        printf("%llu,",tmp->node_id);
			//    }
			if(DEBUG) {
				printAdjList();
			}
			return 0;
		}
		curr = curr->next;
	}
	if(DEBUG) {
		printf("Node %llu does not exist!\n", node_id);
		printAdjList();
	}
	return -1;
}
コード例 #3
0
ファイル: main.c プロジェクト: dhiraj113code/bigraph
int main(int argc, char **argv)
{
/* initialize random seed: */
srand (time(NULL));
int start_index;
FILE *input;
if(argc < 2) {printf("Wrong number of input arguments specified specify the file name\n"); exit(-1);}
char *fname;
fname = (char*)malloc(sizeof(char)*FILE_NAME_SIZE);
fname = argv[1];

//Reading the input file and storing the vertices in adjlist
read_file(fname);

if(DEBUG && MORESTATS) printAdjList();
if(DEBUG) logfile = fopen("debug.log", "w");
if(DEBUG) stackfile = fopen("stack.log", "w");
if(DEBUG)  edgelog = fopen("edge.log", "w");
Aa = fopen("Aa.out", "w");
Ab = fopen("Ab.out", "w");
Ac = fopen("Ac.out", "w");

//All memory allocation and other initialization stuff done here
initializeNallmem();

//Choosing a random start node to start the DFS
start_index = rand() % nVert; //The start node is determined randomnly
rootNode = vertices[start_index]->node;

//Call the the recursive biconnectivity function
//**************************************************************
biconn(rootNode, DUMMY_PARENT);
//**************************************************************

printf("start_node = %d\n", start_index + 1);
printArtPoints();
printBiconnVertices();

/*--------------------------------------------*/
if(DEBUG) fclose(logfile);
if(DEBUG) fclose(stackfile);
if(DEBUG) fclose(edgelog);
fclose(Aa);
fclose(Ab);
fclose(Ac);

if(CHARGING)
{
   if(DEBUG) printf("Number of Vertices = %d\n", nVert);
   if(DEBUG) printf("Number of Edges = %d\n", totalEdges);
   printf("----------------------------------------------------------------------------------------\n");
   printf("Total number of operations charged to all vertices is = %d\n", cv);
   printf("Total number of operations charged to all edges is = %d\n", ce);
   printf("Total number of operations is = %d\n", cv + ce);
   printf("Constant factor vertex = %.2f\n", cv*1.0/nVert);
   printf("Constant factor edge = %.2f\n", ce*2.0/totalEdges);
   printf("----------------------------------------------------------------------------------------\n");
   if(MORESTATS) printVertexCharge();
}
}
コード例 #4
0
ファイル: graph.c プロジェクト: v1nvn/DataStructures
void
printGraph(struct graph *graph) {
	int i;
	for (i = 0; i < graph->n_vertices; i++) {
		printf("%d)", graph->vertices[i].data);
		printAdjList(graph->vertices[i].adjList);
		printf("\n");
	}
}
コード例 #5
0
ファイル: main.c プロジェクト: brunoemori/UEM
//Made by Bruno Emori (RA 88736) & Christian Nakata (RA90558)
//Algoritmos em Grafos - Prof Rodrigo Calvo
//Universidade Estadual de Maringá - 2016
int main() {
  int nVertex, nEdges, graphType, v1, v2, edgeWeight, count;
  fscanf(stdin, "%i", &nVertex);
  fscanf(stdin, "%i", &nEdges);
  fscanf(stdin, "%i", &graphType);

  if ((graphType != 0) && (graphType != 1)) {
    printf("Graph type must be (0) - Undirected graph, or (1) - Directed graph.\n");
    return 0;
  }
 
  adjListBlock *adjList[nVertex];
  createAdjList(adjList, nVertex);

  while (fscanf(stdin, "%i", &v1) != EOF) {
    fscanf(stdin, "%i", &v2);
    fscanf(stdin, "%i", &edgeWeight);
    insertAdjList(adjList, v1, v2, edgeWeight);
    if (!graphType)
     insertAdjList(adjList, v2, v1, edgeWeight);
  }

  printf("Number of vertices: %i.\n", nVertex);
  printf("Number of edges: %i.\n", nEdges);
  if (!graphType)
    printf("Graph Type: Undirected Graph\n");
  else
    printf("Graph Type: Directed Graph\n");
  
  printf("\nEdges:\n");
  for (count = 0; count < nVertex; count++) {
    printf("Vertex %i: ", count);
    printAdjList(adjList, count);
    printf("\n");
  }

  printf("\n\nDeep First Search:\n");
  deepFirstSearch(adjList, nVertex);

  printf("\n\nBreadth First Search:\n");
  breadthFirstSearch(adjList, nVertex, 0);

  printf("\n\nComponents:\n");
  connectedComponent(adjList, nVertex);
  
  if (graphType) {
    printf("\n\nShortest path: (Using Dijkstra's Algorithm)\n");
    dijkstra(adjList, nVertex, 0);
  }
  
  printf("End Program.\n");
  return 0;
}
コード例 #6
0
int add_node(uint64_t node_id) {
	if(DEBUG) {
		printf("Start the process of adding node %llu...\n", node_id);
	}
	// check the node already exists or not
	// If already exists, return -1
	// Else prev will be set as the tail of the node
	// printf("*Breakpoint0\n");
	AdjList *curr = graph->adjList;
	AdjList *prev = graph->adjList;
	if(curr == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	//skip the dummy node
	curr = curr->next;
	while(curr != NULL) {
		if(curr->node_id == node_id) {
			if(DEBUG) {
				printf("Node %llu alreay exists!\n", node_id);
			}
			return -1;
		}
		prev = curr;
		curr = curr->next;
	}

	// add node
	if(DEBUG) {
		printf("Now adding node %llu...\n", node_id);
	}
	prev->next = (AdjList *)malloc(sizeof(AdjList));
	prev = prev->next;
	
	graph->V += 1;
	prev->key = graph->V;
	prev->head = (AdjListNode *)malloc(sizeof(AdjListNode));
	prev->head->node_id = 0;
	prev->head->next = NULL;
	prev->head->key = 0;

	prev->next = NULL;
	prev->node_id = node_id;
	if(DEBUG) {
		printf("Adding node %llu completed!\n", node_id);
		printAdjList();
	}
	return 0;
}
コード例 #7
0
ファイル: main.c プロジェクト: Alecs94/DSA-lab
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    printAdjMatrix();
    adjList=createAdjList();
    printAdjList(adjList);
    bfs(0);
    dfs(0);
    printf("\n");
    printf("DFS Recursive\n");
    dfsRecurs(0);
    printf("\n");
    recreateAdjMatrix();
    printAdjMatrixAfter();
    return 0;
}
void main()
{
	int capacity=VertexNum;	 // 顶点个数
	AdjList adjList; // 邻接表
	int row=VertexNum, i, j;	 							

	int adjArray[VertexNum][VertexNum] = 
	{
		{0, 1, 0, 1, 0, 0, 0},
		{1, 0, 1, 0, 0, 0, 0},
		{0, 1, 0, 1, 0, 0, 1},
		{1, 0, 1, 0, 1, 1, 0},
		{0, 0, 0, 1, 0, 1, 0},
		{0, 0, 0, 1, 1, 0, 0},
		{0, 0, 1, 0, 0, 0, 0},
	};
	
	// init adjacency list.
	adjList = init(capacity);	
	if(adjList==NULL)
	{
		return;
	}			
	printf("\n\n\t === reviww for DFS applie into biconnectivity graph ===");
	printf("\n\t === build adjacency list ===\n");	 
	for(i=0;i<row;i++)
	{		
		visited[i]=0;
		for(j=0; j<row; j++)
		{
			if(adjArray[i][j])
			{
				insertAdjList(adjList, i, j+1, adjArray[i][j]); // 插入节点到邻接表.(无向图权值为全1)
			}
		}
	}
	printAdjList(adjList);
	
	// 使用dfs 遍历无向图.
	dfs_find_articulation(adjList, 1, 1);	

	printf("\n === low array ===\n ");
	printArray(low, VertexNum+1);

}
コード例 #9
0
ファイル: main.c プロジェクト: Alecs94/DSA-lab
int main()
{
    FILE * f = fopen("matrix.txt","r");
    readFromAdjMatrix(f);
    printAdjMatrix();
    bfs(0);
    dfs(0);
    dfsRecurs(0);
    prim(0);
    bellman(0);
    kruskal();
    printAdjMatrix();
    dijkstra(0);
    matrixToList();
    printAdjList();
    dfs(0);
    bfs(0);
    dfsRecurs(0);
    listToMatrix();
    printAdjMatrix();
    prim(0);
    dijkstra(0);
    return 0;
}
コード例 #10
0
ファイル: adjlist.c プロジェクト: aakashshah/Dijkstra
/* Build an adjacency list using random variables.
Returns OK on success and ERR on failure */
int buildRandomAdjList(AdjList *pstAdjList)
{
	int noOfEdges;
	int i, vertex1, vertex2, distance;
	//AdjList *pstTmpList, *pstTmpList2;
	
	#if 0
	FILE *pFile;
	char output[50];
	
	pFile = fopen("random.txt", "w");
	if (NULL_PTR == pFile)
	{
		myLog(ERROR, "Random file creation failed!");
		return ERR;
	}
	#endif

	srand(time(NULL));

	// edges in graph = total edges * density%.
	noOfEdges = ((gNoOfVertex) * (gNoOfVertex - 1));
	noOfEdges = ((noOfEdges * gGraphDensity) / 100);

	myLog(DEBUG, "Vertices: [%d], Edges: "
		"[%d] Density: [%d]", gNoOfVertex, noOfEdges, gGraphDensity);
		
	myLog(INFO, "Building graph randomly with [%d] vertices and [%d] density ([%d] edges).",
		gNoOfVertex, gGraphDensity, noOfEdges);

	/* The following loop builds the graph by adding
	edges in the adjacency list */
	for (i = 0; i < noOfEdges; i++)
	{
		vertex1 = 0;
		vertex2 = 0;

		/* randomly get a end vertex. if the pair exists,
		change both vertex1 and vertex2 and try again. &distance is dummy */
		while ((vertex2 == vertex1)
			|| (FALSE != doesPairExistsInAdjList(pstAdjList, vertex1, vertex2, &distance)))
		{
			//randomly get a start and end vertex
			vertex1 = randomWithRange(0, gNoOfVertex-1);
			vertex2 = randomWithRange(0, gNoOfVertex-1);
		}

		distance = randomWithRange(1, MAX_EDGE_COST);

		/* As we have got all 3 variables, add to AdjList */
		if (NULL_PTR == addToAdjList(pstAdjList, vertex1, vertex2, distance))
		{
			myLog(ERR, "addToAdjList failed!");
			return ERR;
		}

		myLog(DEBUG, "%d: Adding (%d, %d) = %d", i, vertex1, vertex2, distance);
		
		//sprintf(output, "%d %d %d\n", vertex1, vertex2, distance);
		//fputs(output, pFile);
	}

	/********* UNCOMMENT TO CHECK ADJ LIST *************/
	/******* use vertex = 5, density = 30 *************/
	#if 0
	/* Print Adj Matrix and double check */
	printAdjList(pstAdjList);
	#endif
	
	//fclose(pFile);
	
	return OK;
}
コード例 #11
0
int shortest_path(uint64_t node_a_id, uint64_t node_b_id) {
	/*
	return code:
	-1: error
	-2: either node does not exist
	>=0 length
	*/
	//find node_a_id and calculate length
	if(DEBUG) {
		printf("Start the process of calculate shortest_path between node %llu and node %llu...\n", node_a_id, node_b_id);
		// printf("*Breakpoint0\n");
		printAdjList();
	}
	if(node_a_id == node_b_id) {
		return 0;
	}
	AdjList *currAdjList = graph->adjList;
	AdjList *headOfA = NULL;
	AdjList *headOfB = NULL;
	if(currAdjList == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	currAdjList = currAdjList->next;
	while(currAdjList != NULL) {
		if(currAdjList->node_id == node_a_id) {
			headOfA = currAdjList;
		}
		if(currAdjList->node_id == node_b_id) {
			headOfB = currAdjList;
		}
		// printf("*Breakpoint1\n");
		// printf("*Breakpoint2\n");
		currAdjList = currAdjList->next;
	}
	if(headOfA == NULL || headOfB == NULL) {
		if(DEBUG) {
			printf("NodeA or(and) NodeB does not exists!\n");
			printAdjList();
		}
		return -2;
	}

	int dist[graph->V];
	for(int i = 1; i <= graph->V; i++) {
		dist[i] = -1;
	}
	dist[headOfA->key] = 0;

	int visited[graph->V];
	for(int i = 1; i <= graph->V; i++) {
		visited[i] = 0;
	}

	uint64_t p = headOfA->key;
	AdjList *headOfP = NULL;

	// printf("Breakpoint0\n");
	while(p != headOfB->key) {
		visited[p] = 1;
		currAdjList = graph->adjList;
		while(currAdjList != NULL) {
			if(currAdjList->key == p) {
				headOfP = currAdjList;
				break;
			}
			currAdjList = currAdjList->next;
		} 
		// printf("Breakpoint1 %llu\n", headOfP->key);
		AdjListNode *tmp = headOfP->head;
		if(tmp == NULL) {
			printf("Error\n");
		}
		tmp = tmp->next;
		while(tmp != NULL) {
			if(visited[tmp->key] == 0 && (dist[tmp->key] == -1 || dist[tmp->key] > dist[headOfP->key] + 1)) {
				// printf("Breakpoint2: %llu\n", tmp->key);
				dist[tmp->key] = dist[headOfP->key] + 1;
			}
			tmp = tmp->next;
		}
		int min = graph->V + 1;
		int minKey = -1;
		for(int i = 1; i <= graph->V; i++) {
			if(visited[i] == 0 && dist[i] != -1 && dist[i] < min) {
				min = dist[i];
				minKey = i;
			}
		}
		p = minKey;
		// printf("Breakpoint3: %llu\n", p);
		if(minKey == -1) {
			return -1;
		}
	}
	// printf("%d\n", graph->V);

	// for(int i = 0; i <= graph->V; i++) {
	// 	printf("%d\n", dist[i]);
	// }
	return dist[headOfB->key];
}
コード例 #12
0
int get_edge(uint64_t node_a_id, uint64_t node_b_id){
	if(DEBUG) {
		printf("Start the process of getting edge between %llu, %llu...\n", node_a_id, node_b_id);
	}
	if(node_a_id == node_b_id) {
		if(DEBUG) {
			printf("Edge between %llu and %llu does not exist!\n", node_a_id, node_b_id);
		}
		return -1;
	}
	
	AdjList *currAdjList = graph->adjList;
	AdjList *headOfA = NULL;
	AdjList *headOfB = NULL;
	if(currAdjList == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	currAdjList = currAdjList->next;
	while(currAdjList != NULL) {
		if(currAdjList->node_id == node_a_id) {
			headOfA = currAdjList;
		}
		if(currAdjList->node_id == node_b_id) {
			headOfB = currAdjList;
		}
		if(headOfA != NULL && headOfB != NULL) {
			break;
		}
		// printf("*Breakpoint1\n");
		// printf("*Breakpoint2\n");
		currAdjList = currAdjList->next;
	}
	if(headOfA == NULL || headOfB == NULL) {
		if(DEBUG) {
			printf("NodeA or(and) NodeB does not exists!\n");
			printAdjList();
		}
		return -1;
	}
	// get edge from A:
	if(DEBUG) {
		printf("Start to find B in A's adjandency list...\n");
	}
	AdjListNode *currAdjListNode = headOfA->head;
	AdjListNode *prevAdjListNode = currAdjListNode;
	// printf("Breakpoint0\n");
	if(currAdjListNode == NULL) {
		printf("currAdjListNode == NULL. Something wired happens...\n");
		return -1;
	}
	currAdjListNode = currAdjListNode->next;
	while(currAdjListNode != NULL) {
		// printf("Breakpoint1\n");
		if(currAdjListNode->node_id == node_b_id) {
			break;
		}
		prevAdjListNode = currAdjListNode;
		currAdjListNode = currAdjListNode->next;
	} 
	if(currAdjListNode == NULL) {
		if(DEBUG) {
			printf("Edge between %llu and %llu does not exist!\n", node_a_id, node_b_id);
			printAdjList();
		}
		return -1;
	}
	if(DEBUG) {
		printf("Edge between %llu and %llu exists!\n", node_a_id, node_b_id);
		printAdjList();
	}
	return 0;
}
コード例 #13
0
int add_edge(uint64_t node_a_id, uint64_t node_b_id){
	if(DEBUG) {
		printf("Start the process of adding edge between %llu, %llu...\n", node_a_id, node_b_id);
	}
	/* return value:
		1. node_a_id == node_b_id : -1
		2. a or b does not exist: -2
		3. edge between a and b already exists: -3
		4. success: 0
	*/
	if(node_a_id == node_b_id) {
		if(DEBUG) {
			printf("Cannot add edge to the single node %llu!\n", node_a_id);
		}
		return -1;
	}
	AdjList *currAdjList = graph->adjList;
	AdjList *headOfA = NULL;
	AdjList *headOfB = NULL;
	if(currAdjList == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	currAdjList = currAdjList->next;
	while(currAdjList != NULL) {
		if(currAdjList->node_id == node_a_id) {
			headOfA = currAdjList;
		}
		if(currAdjList->node_id == node_b_id) {
			headOfB = currAdjList;
		}
		if(headOfA != NULL && headOfB != NULL) {
			break;
		}
		// printf("*Breakpoint1\n");
		// printf("*Breakpoint2\n");
		currAdjList = currAdjList->next;
	}
	if(headOfA == NULL || headOfB == NULL) {
		if(DEBUG) { 
			printf("NodeA or(and) NodeB does not exists!\n");
			printAdjList();
		}
		return -2;
	}
	// Add edge to A:
	if(DEBUG) {
		printf("Start to add B to A's adjandency list...\n");
	}
	AdjListNode *currAdjListNode = headOfA->head;
	AdjListNode *prevAdjListNode = currAdjListNode;
	// printf("Breakpoint0\n");
	if(currAdjListNode == NULL) {
		if(DEBUG) {
			printf("currAdjListNode == NULL. Something wired happens...\n");
		}
		return -3;
	}
	currAdjListNode = currAdjListNode->next;
	while(currAdjListNode != NULL) {
		// printf("Breakpoint1\n");
		if(currAdjListNode->node_id == node_b_id) {
			if(DEBUG) {
				printf("Edge bewteen node a and node b alreay exists\n");
				printAdjList();
			}
			
			return -3;
		}
		prevAdjListNode = currAdjListNode;
		currAdjListNode = currAdjListNode->next;
	} 
	prevAdjListNode->next = (AdjListNode *)malloc(sizeof(AdjListNode));
	prevAdjListNode = prevAdjListNode->next;
	prevAdjListNode->node_id = node_b_id;
	prevAdjListNode->key = headOfB->key;
	prevAdjListNode->next = NULL;
	if(DEBUG) {
		printf("Completed adding B to A's adjandency list!\n");
	}
	if(DEBUG) {
		printf("Start to add B to A's adjandency list...\n");
	}
	currAdjListNode = headOfB->head;
	prevAdjListNode = currAdjListNode;
	// printf("Breakpoint0\n");
	if(currAdjListNode == NULL) {
		printf("currAdjListNode == NULL. Something wired happens...\n");
		return -3;
	}
	currAdjListNode = currAdjListNode->next;
	while(currAdjListNode != NULL) {
		// printf("Breakpoint1\n");
		if(currAdjListNode->node_id == node_a_id) {
			if(DEBUG) {
				printf("Edge bewteen node a and node b alreay exists\n");
			}
			return -3;
		}
		prevAdjListNode = currAdjListNode;
		currAdjListNode = currAdjListNode->next;
	} 
	prevAdjListNode->next = (AdjListNode *)malloc(sizeof(AdjListNode));
	prevAdjListNode = prevAdjListNode->next;
	prevAdjListNode->node_id = node_a_id;
	prevAdjListNode->key = headOfA->key;
	prevAdjListNode->next = NULL;
	if(DEBUG) { 
		printf("Completed adding B to A's adjandency list!\n");
		printAdjList();
	}
	
	return 0;
}
コード例 #14
0
int remove_node(uint64_t node_id) {
	if(DEBUG) { 
		printf("Start the process of removing node %llu...\n", node_id);
	}
	AdjList *curr = graph->adjList;
	AdjList *prev = curr;
	if(curr == NULL) {
		printf("curr == NULL!\n");
		return -1;
	}
	curr = curr->next;
	while(curr != NULL) {
		if(curr->node_id == node_id) {
			break;
		}
		// printf("*Breakpoint1\n");
		prev = curr;
		// printf("*Breakpoint2\n");
		curr = curr->next;
	}
	if(curr == NULL) {
		if(DEBUG) {
			printf("Node %llu does not exist!\n", node_id);
			printAdjList();
		}
		return -1;
	}
	else {
		if(DEBUG) {
			printf("Now deleting node %llu...\n", node_id);
		}
		prev->next = curr->next;
		if(DEBUG) {
			printf("Deleting all the edges that connected to node %llu...\n", node_id);
			curr = graph->adjList;
			if(curr == NULL) {
				printf("curr == NULL!\n");
				return -1;
			}
			curr = curr->next;
			while(curr != NULL) {
				AdjListNode *currAdjListNode = curr->head;
				AdjListNode *prevAdjListNode = currAdjListNode;
				if(currAdjListNode == NULL) {
					printf("curr == NULL!\n");
					return -1;
				}
				currAdjListNode = currAdjListNode -> next;
				while(currAdjListNode != NULL) {
					if(currAdjListNode -> node_id == node_id) {
						break;
					}
					prevAdjListNode = currAdjListNode;
					currAdjListNode = currAdjListNode -> next;
				}
				if(currAdjListNode != NULL) {
					prevAdjListNode->next = currAdjListNode->next;
				}
				curr = curr -> next;
			}
		}
		// graph->V -= 1;
		if(DEBUG) {
			printf("Deleting node %llu completed!\n", node_id);
			printAdjList();
		}
		return 0;
	}

}