Thread: Direct graph in C: select a vertex

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    1

    Direct graph in C: select a vertex

    Hi guys.
    I have tried to write a code in C for a directed graph:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define VOL 20
    
    /* Adjacency Matrix */
    int source_vert, V, E, visited[VOL], graph[VOL][VOL];
    
    void dfs(int i)
    {
        int j;
        visited[i]=1;
        printf("%d", i+1);
        for(j=0;j<V;j++)
        {
            if(graph[i][j]==1&&visited[j]==0)
                dfs(j);
        }
    }
    
    int main()
    {
        int i, j, vertex1, vertex2;
    
        printf("Directed graph\n");
        printf("Please, enter the number of vertices:\n");
        scanf("%d",&V);
        printf("Please, enter the number of edges:\n");
        scanf("%d", &E);
        for(i=0;i<V;i++)
        {
            for(j=0;j<V;j++)
                graph[i][j]=0;
        }
    
        /* Creating edges */
        for(i=0;i<E;i++)
        {
            printf("Please, enter the edges(format: vertex1 vertex2):");
            scanf("%d %d", &vertex1, &vertex2);
            graph[vertex1-1][vertex2-1]=1;
        }
        for(i=0;i<V;i++)
        {
            for(j=0;j<V;j++){
                printf("%d", graph[i][j]);
                printf("\n");
            }
            printf("Enter the source vertex: \n");
            scanf("%d", &source_vert);
            dfs(source_vert-1);
        }
        return 0;
    }

    Something was wrong.
    My adjacency matrix is not a proper matrix, but an array
    Furthermore, I need to select a vertex, in order to randomly select a new neighbour for it.
    Could you please help me? Thank you.

    [/FONT]
    Last edited by Salem; 03-24-2017 at 11:29 AM. Reason: fixed tag wreckage, please use "paste as text" next time

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please help!! Vertex Coloring Algorithm
    By Watervase Chew in forum C Programming
    Replies: 15
    Last Post: 05-27-2013, 10:55 PM
  2. OpenGL Vertex Shader
    By synhyborex in forum C++ Programming
    Replies: 1
    Last Post: 11-04-2012, 07:03 PM
  3. Vertex Arrays
    By Shamino in forum Game Programming
    Replies: 2
    Last Post: 01-08-2006, 01:24 AM
  4. Vertex Arrays
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 11-05-2005, 01:04 AM
  5. Vertex buffer problems, I think?
    By frenchfry164 in forum Game Programming
    Replies: 9
    Last Post: 03-24-2003, 06:03 PM

Tags for this Thread