Cod sursa(job #2741531)

Utilizator stefan.lascuzeanuLascuzeanu Stefan-Andrei stefan.lascuzeanu Data 16 aprilie 2021 12:42:00
Problema Parcurgere DFS - componente conexe Scor 10
Compilator c-64 Status done
Runda Arhiva educationala Marime 2.87 kb
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//POINTERS/VERTEX
typedef struct vertex {
	int name;
	struct vertex** neighbors;
	int numNeighbors;
} vertex;

typedef struct graphVertex {
	int numNodes;
	int numEdges;
	vertex* nodes;
} graphVertex;

int stack[100];
int indexStack = 0;

void push(int value)
{
	if (indexStack < 100)
		stack[indexStack++] = value;
	else
		printf("Dimensiune stiva depasita!\n");
}

int pop()
{
	//verificare daca sunt elemente in stiva
	if (indexStack > 0) {
		int value = stack[--indexStack];
		stack[indexStack] = 0;
		return value;
	}
	printf("Stiva este goala!\n");
	return (int)0;
}

void dfs(graphVertex graph, int here)
{
	push(here);
	int s;
	while (indexStack > 0) {
		// Pop a vertex from stack and print it
		s = pop();

		// Stack may contain same vertex twice. So
		// we need to print the popped item only
		// if it is not visited.
		if (graph.nodes[s].name != -1) {
			graph.nodes[s].name = -1;
		}

		// Get all adjacent vertices of the popped vertex s
		// If a adjacent has not been visited, then push it
		// to the stack.
		for (int k = 0; k < graph.nodes[here].numNeighbors; k++) {
			if (graph.nodes[here].neighbors[k]->name != -1) {
				push(graph.nodes[here].neighbors[k]->name);
			}
		}
	}
}

graphVertex readGraphVertex(const char* fileName)
{
	graphVertex graph;
	graph.numEdges = 0;
	graph.numNodes = 0;
	FILE* f = fopen(fileName, "r");
	if (f == NULL)
		return graph;

	fscanf(f, "%i %i\n", &(graph.numNodes), &(graph.numEdges));
	graph.nodes = (vertex*)malloc(sizeof(vertex) * graph.numNodes);
	if (graph.nodes == NULL)
		return graph;
	for (int i = 0; i < graph.numNodes; i++) {
		graph.nodes[i].name = i;
		graph.nodes[i].numNeighbors = 0;
		graph.nodes[i].neighbors = NULL;
	}
	int a, b;
	for (int i = 0; i < graph.numEdges; i++) {
		fscanf(f, "%i %i", &a, &b);
		a--;
		b--;
		if (graph.nodes[a].numNeighbors == 0) {
			graph.nodes[a].neighbors = (vertex**)malloc(sizeof(vertex*));
		}
		graph.nodes[a].neighbors = (vertex**)realloc(graph.nodes[a].neighbors, sizeof(vertex**) * (++graph.nodes[a].numNeighbors));
		graph.nodes[a].neighbors[graph.nodes[a].numNeighbors - 1] = &(graph.nodes[b]);
		if (graph.nodes[b].numNeighbors == 0) {
			graph.nodes[b].neighbors = (vertex**)malloc(sizeof(vertex*));
		}
		graph.nodes[b].neighbors = (vertex**)realloc(graph.nodes[b].neighbors, sizeof(vertex**) * (++graph.nodes[b].numNeighbors));
		graph.nodes[b].neighbors[graph.nodes[b].numNeighbors - 1] = &(graph.nodes[a]);
		//&(graph.nodes[b]);
	}
	fclose(f);
	return graph;
}

int main()
{
	graphVertex graphV = readGraphVertex("dfs.in");
	int ct = 0;
	for (int i = 0; i < graphV.numNodes; i++) {
		if (graphV.nodes[i].name != -1) {
			dfs(graphV, i);
			ct++;
		}
	}
	FILE* o = fopen("dfs.out", "w");
	fprintf(o, "%d", ct);
	fclose(o);
	return 0;
}