예제 #1
0
void create_graph_reconsidered(Graph *graph_reconsidered,Graph *graph){
    for(int i = 0; i < graph->numbers_nodes; i++)
        for(int j = 0; j < graph->numbers_nodes; j++)
            insert_arc(graph_reconsidered, i, j, get_cost_edge(graph, i, j));
    for(int i = 0; i < graph->numbers_nodes; i++)
        insert_arc(graph_reconsidered, graph_reconsidered->numbers_nodes-1, i, 0);
};
예제 #2
0
// read a graph from the given file, return success
bool t_dir_graph::read_format1(istream& input, const string first_line){
	string arcs;
	vector<t_vertex> vert_vec;
	vector<t_vertex> arc_vec;

	input >> arcs;
	tokenize(first_line, vert_vec, ",");
	tokenize(arcs, arc_vec, "),(");

	for(uint i=0; i < vert_vec.size(); i++)
		insert_vertex(vert_vec[i]);
	size_t arrow_pos = 0;
	for(uint i=0; i < arc_vec.size(); i++){
		if((arrow_pos = arc_vec[i].find("->")) != string::npos){
			dbgcout << "arc: " << arc_vec[i].substr(0,arrow_pos) << "," << arc_vec[i].substr(arrow_pos+2) << "\n";
			insert_arc(t_arc(arc_vec[i].substr(0,arrow_pos),arc_vec[i].substr(arrow_pos+2)));
		} else {
			i++;
			if(i < arc_vec.size()){
				dbgcout << "arc: " << arc_vec[i-1] << "," << arc_vec[i] << "\n";
				insert_arc(t_arc(arc_vec[i-1],arc_vec[i]));
			}
		}
	}

	return true;
}
예제 #3
0
bool t_dir_graph::read_format2(istream& input, const string first_line){
	vector<t_vertex> verts;
	int adj;
	t_vertex v;
	uint datas = atoi(first_line.c_str());

	while(input.peek() == '\n') input.ignore(1); // read the '\n'
	// read vertices
	for(uint i = 0; i < datas; i++){
		input >> v;
		verts.push_back(v);
		while(input.peek() == '\n') input.ignore(1); // read the '\n'
	}
	for(uint i=0; i < verts.size(); i++) insert_vertex(verts[i]);
	// read adjacency matrix
	for(uint i=0; i < verts.size(); i++){
		for(uint j=0; j < verts.size(); j++){
			input >> adj;
			if(adj > 0)
				insert_arc(t_arc(verts[i], verts[j]));
			while(input.peek() == ' ') input.ignore(1); // read the ' '
			while(input.peek() == '\n') input.ignore(1); // read the '\n'
		}
	}
	return true;
}
예제 #4
0
파일: graph.c 프로젝트: vitorfs/graph
int main(int argc, char* argv[]) {
  Adjacency_Matrix* g = (Adjacency_Matrix*) malloc(sizeof(Adjacency_Matrix));
  Adjacency_Matrix* tp;
  int* adjacency;
  empty_graph(g);

  int option, v, a1, a2;

  do {
    menu();
    scanf("%d", &option);
    switch (option) {
      case INSERT_VERTEX:
        printf("How many vertex would you like to insert? ");
        scanf("%d", &v);
        insert_vertex(g, v);
        break;
      case REMOVE_VERTEX:
        printf("Which vertex would you like to remove? ");
        scanf("%d", &v);
        remove_vertex(g, v);
        break;
      case INSERT_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        insert_arc(g, a1, a2, 1);
        break;
      case REMOVE_ARC:
        printf("First vertex: ");
        scanf("%d", &a1);
        printf("Second vertex: ");
        scanf("%d", &a2);
        remove_arc(g, a1, a2);
        break;
      case VERTEX_ADJACENCY:
        printf("Which vertex would you like to verify adjacency?");
        scanf("%d", &v);
        adjacency = get_adjacency(g, v);
        print_adjacency(adjacency);
        free(adjacency);
        pause();
        break;
      case TRANSPOSE_GRAPH:
        tp = transpose_graph(g);
        print_graph(tp);
        free(tp);
        pause();
        break;
      case PRINT_GRAPH: 
        print_graph(g);
        pause();
        break;
    }
  } while (option != EXIT);

  return 0;
}
예제 #5
0
/*
 * This happend to be our first approuch to get the maximum matching.
 * THe results are pretty satisfatory and the code complexity isnt that bad at all
 */
Graph* maximal_matching(Graph* g) {
  Graph* m = (Graph*) malloc(sizeof(Graph));
  Graph* matching = (Graph*) malloc(sizeof(Graph));

  init_graph(m, g->vertex_count);
  init_graph(matching, g->vertex_count); 

  int i;
  int j;

  // Make a copy of the graph ignoring loops
  for (i = 0 ; i < g->vertex_count ; i++)
    for (j = 0 ; j < g->vertex_count ; j++)
      if (i != j)
        m->arcs[i][j] = g->arcs[i][j];

  Vertex* v = (Vertex*) malloc(g->vertex_count * sizeof(Vertex));
  get_ordered_vertex(m, v);
  
  int saturated[m->vertex_count];
  for (i = 0 ; i < m->vertex_count ; i++)
    saturated[i] = 0;

  for (i = 0 ; i < m->vertex_count ; i++) {
    m->vertex[i].degree = get_vertex_degree(m, i);
  }

  int* adj;
  for (i = 0 ; i < m->vertex_count ; i++) {
    int v1 = v[i].vertex;
    int v2;

    if (saturated[v1] == 0) {
      adj = get_adjacency(m, v1);
      if (adj[0] > 0) {
        v2 = adj[1];
        
        for (j = 2 ; j <= adj[0] ; j++) {
          if (m->vertex[adj[j]].degree < m->vertex[v2].degree) 
            v2 = adj[j];
        }
        
        insert_arc(matching, v1, v2, m->arcs[v1][v2]);
        free(adj);
        saturated[v1] = 1;
        saturated[v2] = 1;
        
        for (j = 0 ; j < m->vertex_count ; j++) {
          remove_arc(m, v1, j);
          remove_arc(m, v2, j);
        }
      }
    }
  }
  
  return matching;
}
예제 #6
0
BellmanFordResult* reconsider_graph(Graph *graph){
    BellmanFordResult *bellman_ford_result;
    Graph graph_reconsidered;
    graph_reconsidered.numbers_nodes = graph->numbers_nodes + 1;
    allocate_memory(&graph_reconsidered);
    create_graph_reconsidered(&graph_reconsidered, graph);
    bellman_ford_result = bellmanFord(&graph_reconsidered, graph_reconsidered.numbers_nodes-1);
    if(bellman_ford_result->exist_negative_cycle == false){
        for(int i = 0; i < graph->numbers_nodes; i++)
            for(int j = 0; j < graph->numbers_nodes; j++)
                if(exist_arc(graph, i, j))
                    insert_arc(graph, i, j, get_cost_edge(graph, i, j) + bellman_ford_result->distance[i] - bellman_ford_result->distance[j]);
    };
    return bellman_ford_result;
};
예제 #7
0
파일: arcs.cpp 프로젝트: DanielAM2013/VRP
// Segunda questão
 void insert( vertex o, vertex d) {
  insert_arc( o, d);
  insert_arc( d, o);
 }