Thread: Depth first search - Homework

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    1

    Depth first search - Homework

    Hello,
    I have a problem with doing my programming homework. I am probably completely lost in everything about linked lists and stuff like that...
    Teacher said it should be easy, but I don't understand the basics I think, so if you would please tell me how to search the graph that is created in the code with DFS I would be really grateful.
    It should search the graph from startNodeId to endNodeId.
    Don't mind the notes in the text

    Code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define NODE_COUNT 20
    #define NEIGHBOURS_SIZE 7
    /***************************************************
    Implementace grafu
    
    
    funkce createGraph vytváří graf, ve kterém se má hledat
    funkce destroyGraph graf uklidí z paměti
    ****************************************************/
    
    
    typedef struct TNODE{
        int mId; //identifikace uzlu
        int mNeighbourIds[NEIGHBOURS_SIZE]; //id sousedu
    } TNode;
    
    int randomNodeId(){
        int i;
        i = rand();
        i %= NODE_COUNT;
        return i;
    }
    
    void createGraph(TNode ** nodes){
        //init
        for(int i = 0; i < NODE_COUNT; i++){
            TNode * newNode;
            newNode = (TNode*)malloc(sizeof(TNode));
            newNode -> mId = i;
            for(int j = 0; j < NEIGHBOURS_SIZE; j++){
                newNode -> mNeighbourIds[j] = -1; //-1 rika ze ta pozice je volna, az v tom budes hledat, resp. na pozicich [0] a [1] bude neco > -1, tak to ukazuje na dva sousedy, ten zbytek neukazuje nikam
            }
            nodes[i] = newNode;
        }
        //create edges
        nodes[0] -> mNeighbourIds[0] = 7;
        nodes[0] -> mNeighbourIds[1] = 2;
        nodes[0] -> mNeighbourIds[2] = 10;
        nodes[0] -> mNeighbourIds[3] = 11;
        nodes[0] -> mNeighbourIds[4] = 18;
        nodes[1] -> mNeighbourIds[0] = 8;
        nodes[1] -> mNeighbourIds[1] = 15;
        nodes[1] -> mNeighbourIds[2] = 11;
        nodes[1] -> mNeighbourIds[3] = 9;
        nodes[2] -> mNeighbourIds[0] = 0;
        nodes[2] -> mNeighbourIds[1] = 14;
        nodes[2] -> mNeighbourIds[2] = 8;
        nodes[3] -> mNeighbourIds[0] = 9;
        nodes[3] -> mNeighbourIds[1] = 12;
        nodes[4] -> mNeighbourIds[0] = 5;
        nodes[4] -> mNeighbourIds[1] = 17;
        nodes[4] -> mNeighbourIds[2] = 13;
        nodes[5] -> mNeighbourIds[0] = 4;
        nodes[5] -> mNeighbourIds[1] = 18;
        nodes[6] -> mNeighbourIds[0] = 17;
        nodes[6] -> mNeighbourIds[1] = 12;
        nodes[7] -> mNeighbourIds[0] = 0;
        nodes[7] -> mNeighbourIds[1] = 15;
        nodes[7] -> mNeighbourIds[2] = 14;
        nodes[8] -> mNeighbourIds[0] = 1;
        nodes[8] -> mNeighbourIds[1] = 2;
        nodes[8] -> mNeighbourIds[2] = 14;
        nodes[9] -> mNeighbourIds[0] = 3;
        nodes[9] -> mNeighbourIds[1] = 1;
        nodes[10] -> mNeighbourIds[0] = 0;
        nodes[10] -> mNeighbourIds[1] = 15;
        nodes[11] -> mNeighbourIds[0] = 0;
        nodes[11] -> mNeighbourIds[1] = 1;
        nodes[11] -> mNeighbourIds[2] = 16;
        nodes[12] -> mNeighbourIds[0] = 3;
        nodes[12] -> mNeighbourIds[1] = 13;
        nodes[12] -> mNeighbourIds[2] = 6;
        nodes[13] -> mNeighbourIds[0] = 4;
        nodes[13] -> mNeighbourIds[1] = 12;
        nodes[14] -> mNeighbourIds[0] = 7;
        nodes[14] -> mNeighbourIds[1] = 8;
        nodes[14] -> mNeighbourIds[2] = 2;
        nodes[15] -> mNeighbourIds[0] = 1;
        nodes[15] -> mNeighbourIds[1] = 10;
        nodes[15] -> mNeighbourIds[2] = 7;
        nodes[16] -> mNeighbourIds[0] = 11;
        nodes[16] -> mNeighbourIds[1] = 19;
        nodes[17] -> mNeighbourIds[0] = 4;
        nodes[17] -> mNeighbourIds[1] = 6;
        nodes[18] -> mNeighbourIds[0] = 0;
        nodes[18] -> mNeighbourIds[1] = 5;
        nodes[19] -> mNeighbourIds[0] = 16;
    }
    
    void destroyGraph(TNode ** nodes){
        for(int i = 0; i < NODE_COUNT; i++){
            free(nodes[i]);
        }
    }
    
    int main(){
        srand((unsigned)time(NULL));
        TNode * map[NODE_COUNT]; //staticky pole ukazatelu na TNode
        int startNodeId = randomNodeId();
        int endNodeId = randomNodeId();
        createGraph(map);
        
        //projdi pomoci bfs a dfs graf ze startNodeId (map[startNodeId]) do endNodeId
        //pokud by ses chtel jo vytahnout muzes k tomu udelat i rekonstrukci cesty
        //neni to ale nutny, staci kdyz to dojde do cile tak jak ma
    
        destroyGraph(map);
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    So where's your attempt at say
    Code:
    dfs(map,startNodeId,endNodeId);
    But before you go for full random nodes, consider an example where the nodes are adjacent.
    Say
    dfs(map,0,7);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. depth-first search of a graph
    By evinda in forum C Programming
    Replies: 1
    Last Post: 05-20-2013, 08:27 AM
  2. Help with Depth First Search
    By [Student] in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2012, 12:28 PM
  3. Depth-first search & TSP
    By Cpro in forum C++ Programming
    Replies: 12
    Last Post: 12-13-2008, 12:51 PM
  4. Depth-First Search using matrices
    By supaben34 in forum C++ Programming
    Replies: 9
    Last Post: 11-02-2002, 01:53 PM

Tags for this Thread