DFS LISTS

 #include <stdio.h>

#include <stdlib.h>


#define MAX 100

#define initial 1

#define visited 2


struct Node {

    int vertex;

    struct Node* next;

};


struct Node* adjList[MAX];  

int state[MAX];     

int stack[MAX], top = -1; 


void create_graph(int n, int gtype);

void add_edge(int src, int dest, int gtype);

void DF_Traversal(int n);

void DFS(int v);

void push(int vertex);

int pop();

int isEmpty_stack();


int main() {

    int n, gtype;

    

    printf("Enter 1 for undirected graph and -1 for directed graph: ");

    scanf("%d", &gtype);

    printf("Enter the number of vertices: ");

    scanf("%d", &n);

    

    create_graph(n, gtype);

    

    DF_Traversal(n);

    

    return 0;

}


void create_graph(int n, int gtype) {

    int max_edges, src, dest;

    

    if (gtype == 1)

        max_edges = (n * (n - 1)) / 2;

    else if (gtype == -1)

        max_edges = n * (n - 1);


    for (int i = 1; i <= max_edges; i++) {

        printf("Enter edge %d (to quit enter -1 -1): ", i);

        scanf("%d%d", &src, &dest);


        if (src == -1 && dest == -1)

            break;


        if (src < 0 || dest < 0 || src >= n || dest >= n) {

            printf("Invalid source or destination!\n");

            i--;

        } else {

            add_edge(src, dest, gtype);

        }

    }


    printf("Adjacency List is:\n");

    for (int i = 0; i < n; i++) {

        struct Node* temp = adjList[i];

        printf("Vertex %d:", i);

        while (temp) {

            printf(" -> %d", temp->vertex);

            temp = temp->next;

        }

        printf("\n");

    }

}


void add_edge(int src, int dest, int gtype) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->vertex = dest;

    newNode->next = adjList[src];

    adjList[src] = newNode;

    

    if (gtype == 1) { 

        newNode = (struct Node*)malloc(sizeof(struct Node));

        newNode->vertex = src;

        newNode->next = adjList[dest];

        adjList[dest] = newNode;

    }

}


void DF_Traversal(int n) {

    int v;

    

    for (v = 0; v < n; v++)

        state[v] = initial;

    

    printf("Enter starting vertex for Depth First Search: ");

    scanf("%d", &v);

    

    DFS(v);

}


void DFS(int v) {

    struct Node* temp;

    push(v);

    state[v] = visited;

    

    printf("DFS Traversal starting from vertex %d:\n", v);

    printf("%d ", v);


    while (!isEmpty_stack()) {

        v = stack[top];

        

        int found = 0; 

        for (temp = adjList[v]; temp != NULL; temp = temp->next) {

            int adjVertex = temp->vertex;

            if (state[adjVertex] == initial) {

                push(adjVertex);

                state[adjVertex] = visited;

                printf("%d ", adjVertex);

                found = 1;

                break;

            }

        }


        if (!found) 

            pop();

    }

    printf("\n");

}


void push(int vertex) {

    if (top == MAX - 1) {

        printf("Stack Overflow\n");

        return;

    }

    stack[++top] = vertex;

}


int pop() {

    if (top == -1) {

        printf("Stack Underflow\n");

        exit(1);

    }

    return stack[top--];

}


int isEmpty_stack() {

    return (top == -1);

}

Comments

Popular posts from this blog

AVL Trees

BFS Matrix

BFS List