int main() {
	Graph graph;
	initGraph(&graph);

	char input[100];

	//read console input and build graph
	while (1) {
		if (fgets(input, 100, stdin)) { //fgets returns NULL at EOF (EOF in VS cmd line: enter ctrl+z enter)
			buildGraphFromInput(input, &graph);
		}
		else
			break;
	}

	//debug only
	buildGraphFromInput("A-BD\n", &graph);
	buildGraphFromInput("B-AC\n", &graph);
	buildGraphFromInput("C-BG\n", &graph);
	buildGraphFromInput("D-AEF\n", &graph);
	buildGraphFromInput("E-DG\n", &graph);
	buildGraphFromInput("F-D\n", &graph);
	buildGraphFromInput("G-CE\n", &graph);

	//traverse graph with BFS
	breadthFirstSearch(&graph);
	printf("\n");

	return EXIT_SUCCESS;
}
Beispiel #2
0
int main(int argc, char **agv) {
    int i;
    int visited[SIZE];
    Graph *graph = initGraph(SIZE);

    printf("%d\n", sizeof(int **));
    graph->edgeMat[0][1] = 1;
    graph->edgeMat[0][2] = 1;
    graph->edgeMat[1][2] = 1;
    graph->edgeMat[2][0] = 1;
    graph->edgeMat[2][3] = 1;
    graph->edgeMat[3][3] = 1;

    for(i = 0; i < graph->V; i++) {
        visited[i] = 0;
    }
    printf("DFS rec ");
    depthFirstSearch(graph, visited, 2);
    
    for(i = 0; i < graph->V; i++) {
        visited[i] = 0;
    }

    printf("\n DFS non_rec ");
    depthFirstSearchNonRec(graph, visited, 2);

    printf("\n BFS non_rec");
    breadthFirstSearch(graph, 2);

    releaseGraph(graph);
    return 0;
}
Beispiel #3
0
void Graph::search(SearchType searchType) {

    vector<Vertex *> vertexes = getVertexes();

    for (vector<Vertex *>::iterator it = vertexes.begin(); it != vertexes.end(); it++) {

        Vertex *vertex = *it;

        vertex->setVisited(false);
    }

    for (vector<Vertex *>::iterator it = vertexes.begin(); it != vertexes.end(); it++) {

        Vertex *vertex = *it;

        if (!vertex->getVisited()) {

            switch (searchType) {

                case DEPTH:
                    depthFirstSearch(vertex);
                    break;

                case BREADTH:
                    breadthFirstSearch(vertex);
                    break;

                case WEIGHT:
                    updateAllWeight(vertex);
            }
        }
    }
}
Beispiel #4
0
int Master::enumerationStrategy(const Sub *s1, const Sub *s2)
{
	switch (enumerationStrategy_) {
	case BestFirst:    return bestFirstSearch(s1, s2);
	case BreadthFirst: return breadthFirstSearch(s1, s2);
	case DepthFirst:   return depthFirstSearch(s1, s2);
	case DiveAndBest:  return diveAndBestFirstSearch(s1, s2);
	default:
		Logger::ifout() << "Master::enumerationStrategy(): Unknown enumeration strategy\n";
		OGDF_THROW_PARAM(AlgorithmFailureException, ogdf::AlgorithmFailureCode::IllegalParameter);
	}
}
Beispiel #5
0
//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;
}
Beispiel #6
0
int main(){

    connect_to_robot();
    initialize_robot();
    set_origin();
    set_ir_angle(LEFT, -45);
    set_ir_angle(RIGHT, 45);
    initialize_maze();
    reset_motor_encoders();
    int i;
    for (i = 0; i < 17; i++){
        set_point(nodes[i]->x, nodes[i]->y);
    }
    double curr_coord[2] = {0, 0};
    map(curr_coord, nodes[0]);
    breadthFirstSearch(nodes[0]);
    reversePath(nodes[16]);
    printPath(nodes[0]);

    struct point* tail = malloc(sizeof(struct point));
    tail->x = nodes[0]->x;
    tail->y = nodes[0]->y;
    struct point* startpoint = tail;

    build_path(tail, nodes[0]);
    
    // Traverse to end node.
    while(tail->next){
        set_point(tail->x, tail->y); // Visual display for Simulator only.
        tail = tail->next;
    }
    tail->next = NULL; // Final node point to null.
    printf("tail: X = %f Y = %f \n", tail->x, tail->y);
    parallel(curr_coord);
    spin(curr_coord, to_rad(180));
    
    sleep(2);
    set_ir_angle(LEFT, 45);
    set_ir_angle(RIGHT, -45);

    mazeRace(curr_coord, nodes[0]);
    return 0;
}
std::vector<State*> SearchTree::doSearch(std::string algorithm)
{
    double startT, finishT;
    #ifdef __unix
    timespec startWallTime, finishWallTime;
    clock_gettime(CLOCK_MONOTONIC, &startWallTime);
    #else
    GET_TIME(startT);
    #endif
    std::clock_t startCpuTime = std::clock();

    if (algorithm == "bcktrk")
        solution = backTracking(root);
    else if (algorithm == "dfs")
        solution = depthFirstSearch(dfsDepthLimit);
    else if (algorithm == "bfs")
        solution = breadthFirstSearch();
    else if (algorithm == "ucs")
        solution = orderSearch();
    else if (algorithm == "greedy")
        solution = greedy();
    else if (algorithm == "astr")
        solution = AStar();
    else if (algorithm == "idastr")
        solution = IDAStar();

    searchCpuTime = (std::clock() - startCpuTime) / (double)CLOCKS_PER_SEC;
    #ifdef __unix
    clock_gettime(CLOCK_MONOTONIC, &finishWallTime);
    searchWallTime = (finishWallTime.tv_sec - startWallTime.tv_sec) + (finishWallTime.tv_nsec - startWallTime.tv_nsec) / 1000000000.0;
    #else
    GET_TIME(finishT);
    searchWallTime = finishT - startT;
    #endif
    return getPathTo(solution);
}
Beispiel #8
0
Vector<TBLoc>
shortestPath(TBLoc start,
             TBLoc end,
             const Grid<double>& world,
             double costFn(const TBLoc& from, const TBLoc& to, const Grid<double>& world),
             double heuristicFn(const TBLoc& from, const TBLoc& to, const Grid<double>& world),
             AlgorithmType algorithm) {
    // modified by Marty to use an actual Graph object
    ensureWorldCache(world, costFn);
    cout << endl;

    Grid<double>* const pWorld = const_cast<Grid<double>*>(&world);
    BasicGraph* graph = WORLD_CACHE[pWorld];
    // graph->resetData();   // make the student worry about this

    s_world = pWorld;
    s_heuristicFunction = heuristicFn;

    // convert start/end from Loc to Vertex
    Vertex* startVertex = graph->getVertex(vertexName(start.row, start.col, world));
    Vertex* endVertex   = graph->getVertex(vertexName(end.row, end.col, world));
    if (startVertex == NULL) {
        error(string("Graph can not find start vertex with name \"") + vertexName(start.row, start.col, world) + "\"");
        for (Vertex* v : graph->getVertexSet()) {
            cout << v->name << " ";
        }
        cout << endl;
    }
    if (endVertex == NULL) {
        error(string("Graph can not find end vertex with name \"") + vertexName(start.row, start.col, world) + "\"");
        for (Vertex* v : graph->getVertexSet()) {
            cout << v->name << " ";
        }
        cout << endl;
    }

    cout << "Looking for a path from " << startVertex->name
         << " to " << endVertex->name << "." << endl;

    Vector<Vertex*> result;
    switch (algorithm) {
    case BFS:
        cout << "Executing breadth-first search algorithm ..." << endl;
        result = breadthFirstSearch(*graph, startVertex, endVertex);
        break;
    case DIJKSTRA:
        cout << "Executing Dijkstra's algorithm ..." << endl;
        result = dijkstrasAlgorithm(*graph, startVertex, endVertex);
        break;
    case A_STAR:
        cout << "Executing A* algorithm ..." << endl;
        result = aStar(*graph, startVertex, endVertex);
        break;
#ifdef BIDIRECTIONAL_SEARCH_ALGORITHM_ENABLED
    case BIDIRECTIONAL:
        cout << "Executing Bidirectional Search algorithm ..." << endl;
        extern Vector<Vertex*> bidirectionalSearch(BasicGraph& graph, Vertex* start, Vertex* end);
        result = bidirectionalSearch(*graph, startVertex, endVertex);
        break;
#endif // BIDIRECTIONAL_SEARCH_ALGORITHM_ENABLED
    case DFS:
    default:
        cout << "Executing depth-first search algorithm ..." << endl;
        result = depthFirstSearch(*graph, startVertex, endVertex);
        break;
    }

    cout << "Algorithm complete." << endl;

    // convert Vector<Vertex*> to Vector<Loc>
    Vector<TBLoc> locResult;
    for (Vertex* v : result) {
        locResult.add(TBLoc(getRow(v), getCol(v)));
    }
    return locResult;
}
Beispiel #9
0
std::list<int>
Graphs::getResult()
{
    // find solution
    int solution = -1;
    bool flag = false;
    for (int i = 0; i < (int) source.size(); ++i)
    {
        solution = breadthFirstSearch(GRASPcells, manip_graph, source[i], target);
        if (solution != -1)
        {
            flag = true;
            break;
        }
    }

    // just print source and target
    std::cout << "************** SOURCES **********" << std::endl;
    for (int i = 0; i < (int) source.size(); ++i)
        std::cout << " " << std::to_string(source[i]);
    std::cout << std::endl;

    std::cout << "************** TARGETS **********" << std::endl;
    for (int i = 0; i < (int) target.size(); ++i)
        std::cout << " " << std::to_string(target[i]);
    std::cout << std::endl;


    if (flag) // if there is a result
    {
        // retrieve solution
        int current = solution;
        do {
            result.push_back(current);
            current = GRASPcells[current].father;
        }
        while (current != -1);

        result.reverse();

        // print it
        std::cout << "************ SOLUTION IS (GRASP Cells) *************" << std::endl;
        for (std::list<int>::iterator it = result.begin(); it != result.end(); ++it)
        {
            std::cout << " >> " << *it;
        }
        std::cout << std::endl;
    }
    else
    {
        std::cout << "************ THERE IS NO SOLUTION *************" << std::endl;
    }


    std::cout << std::endl << "************ Liaison edges *************" << std::endl;
    for (int i = 0; i < (int) GRASPManipCells.size(); ++i)
    {
        if ((int) GRASPManipCells[i].size() > 1)
        {
            for (int j = 0; j < (int) GRASPManipCells[i].size(); ++j)
            {
                std::cout << " " << GRASPManipCells[i][j];
            }
            std::cout << std::endl;
        }

    }

    return result;
}
Beispiel #10
0
int testBinarySearchTree(void) {
	Tree *head = NULL;
	Tree *singleNodeTree = malloc(sizeof(Tree));
	Tree *test[2];	
	if(singleNodeTree == NULL) {
		exit(EXIT_FAILURE);
	}
	singleNodeTree->value = 100;
	singleNodeTree->left = NULL;
	singleNodeTree->right = NULL;

	printf("Start Test of Binary Search Tree.\n");
	printf("Initial contents of the tree:\n");
	print_ascii_tree(head);

	printf("\nadd() 5 to the tree\n");
	head = addToTree(head, 5);
	print_ascii_tree(head);
	printf("\nadd() 2 to the tree\n");
	head = addToTree(head, 2);
	print_ascii_tree(head);
	printf("\nadd() 12 to the tree\n");	
	head = addToTree(head, 12);
	print_ascii_tree(head);
	printf("\nadd() 1 to the tree\n");	
	head = addToTree(head, 1);
	print_ascii_tree(head);
	printf("\nadd() 8 to the tree\n");	
	head = addToTree(head, 8);
	print_ascii_tree(head);
	printf("\nadd() 4 to the tree\n");	
	head = addToTree(head, 4);
	print_ascii_tree(head);
	printf("\nadd() 3 to the tree\n");	
	head = addToTree(head, 3);
	print_ascii_tree(head);
	printf("\nadd() 10 to the tree\n");	
	head = addToTree(head, 10);
	print_ascii_tree(head);
	printf("\nadd() 9 to the tree\n");	
	head = addToTree(head, 9);
	print_ascii_tree(head);	
	printf("\nadd() 11 to the tree\n");	
	head = addToTree(head, 11);
	print_ascii_tree(head);	
	printf("\nadd() 7 to the tree\n");	
	head = addToTree(head, 7);
	print_ascii_tree(head);		

	if( (test[0] = findInTree(head, 3)) == NULL) {
		printf("\nfind(3) = Value Not Found\n");
	} else {
		printf("\nfind(3) = %d\n", test[0]->value);
	}

	if( (test[0] = findInTree(head, 7)) == NULL) {
		printf("\nfind(7) = Value Not Found\n");
	} else {
		printf("\nfind(7) = %d\n", test[0]->value);
	}

	if( (test[0] = findInTree(head, 4)) == NULL) {
		printf("\nfind(4) = Value Not Found\n");
	} else {
		printf("\nfind(4) = %d\n", test[0]->value);
	}

	findParentInTree(head, head, 3, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(3) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 5, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(5) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 2, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(2) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	findParentInTree(head, head, 9, test);
	if( test[1] == NULL) {
		printf("\nfindParentOfNode(9) = Value Not Found\n");
	} else {
		printf("\nfindParentOfNode(%d) = %d\n", test[0]->value, test[1]->value);
	}

	printf("Depth of the tree = %d\n", getTreeDepth(head, 0));
	inOrderTraversal(head);
	preOrderTraversal(head);
	postOrderTraversal(head);	
	breadthFirstSearch(head);		
	/* Delete Order: 1, 3, 12, 8, 5 */
	printf("\nbefore removing any items\n");
	print_ascii_tree(head);		
	printf("\nremove() 1 from the tree\n");	
	head = removeFromTree(head, 1);
	print_ascii_tree(head);		
	printf("\nremove() 3 from the tree\n");	
	head = removeFromTree(head, 3);
	print_ascii_tree(head);		
	printf("\nremove() 12 from the tree\n");	
	head = removeFromTree(head, 12);
	print_ascii_tree(head);		
	printf("\nremove() 8 from the tree\n");	
	head = removeFromTree(head, 8);
	print_ascii_tree(head);		
	printf("\nremove() 5 from the tree\n");	
	head = removeFromTree(head, 5);
	print_ascii_tree(head);			
	printf("\nremove() 11 from the tree\n");	
	head = removeFromTree(head, 11);
	print_ascii_tree(head);				
	printf("Depth of the tree = %d\n", getTreeDepth(head, 0));
	inOrderTraversal(head);
	breadthFirstSearch(head);		

	printf("\nsinglNodeTests start here: \n");
	print_ascii_tree(singleNodeTree);
	inOrderTraversal(singleNodeTree);

	printf("\n remove() 1 from tree\n");
	if(removeFromTree(singleNodeTree, 1) == NULL) {
		printf("Node Not Found, nothing removed\n");
	}
	printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0));	

	printf("\n remove() 100 from tree\n");
	singleNodeTree = removeFromTree(singleNodeTree, 100);
	printf("Depth of the singleNodeTree = %d\n", getTreeDepth(singleNodeTree, 0));

	return EXIT_SUCCESS;
}