SoFunction
Updated on 2025-03-09

Depth priority search example of traversal of graphs in C language

DFS (Depth-First-Search) depth-first search algorithm is a very common type of algorithm in graph traversal algorithms. Share it for your reference. The specific methods are as follows:

#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;  

#define MAX_VERTEX_NUM 10

struct Node
{
 int adjvex;
 struct Node *next;
 int info;
};

typedef struct VNode
{
 char data;
 Node *first;
}VNode, AdjList[MAX_VERTEX_NUM];

struct Graph 
{
 AdjList vertices;
 int vexnum, arcnum;
};

int visited[MAX_VERTEX_NUM];

int locateVex(Graph G, char u)
{
 int i;
 for (i = 0; i < ; i++)
 {
 if (u == [i].data)
  return i;
 }

 if (i == )
 {
 printf("Error u!\n");
 exit(1);
 }

 return 0;
}

void createGraph(Graph &G)
{
 int i, j, k, w;
 char v1, v2, enter;

 Node *p;
 printf("input vexnum & arcnum:\n");
 scanf("%d", &);
 scanf("%d", &);
 printf("input vertices:\n");
 for (i = 0; i < ; i++)
 {
 scanf("%c%c", &enter, &[i].data);
 [i].first = NULL;
 }

 printf("input Arcs(v1, v2, w):\n");
 for (k = 0; k < ; k++)
 {
 scanf("%c%c", &enter, &v1);
 scanf("%c%c", &enter, &v2);
 scanf("%d", &w);
 i = locateVex(G, v1);
 j = locateVex(G, v2);
 p = (Node *)malloc(sizeof(Node));
 p->adjvex = j;
 p->info = w;
 p->next = [i].first;
 [i].first = p;
 }
}

void DFS(Graph &G, int v)
{
 Node *p;
 printf("%c", [v].data);
 visited[v] = 1;
 p = [v].first;

 while (p)
 {
 if (!visited[p->adjvex])
  DFS(G, p->adjvex);
 p = p->next;
 }
}

void DFSTranverse(Graph &G)
{
 for (int v = 0; v < ; v++)
 visited[v] = 0;
 for (int v = 0; v < ; v++)
 {
 if (!visited[v])
  DFS(G, v);
 }
}

int main()
{
 Graph G;
 createGraph(G);
 DFSTranverse(G);
}

Another way to write DFS. The specific code is as follows:

#include <iostream>
#include <string>

using namespace std;

#define MAXLEN 10

struct Node
{
 int data;
 Node *next;
};

struct Link
{
 int count;
 string name;
 Node *head;
};

struct Graph
{
 Link link[MAXLEN];
 int vexnum;
 int arcnum;
};

int findIndex(Graph &G, string name)
{
 int index = -1;

 for (int i = 0; i < ; i++)
 {
 if ([i].name == name)
 {
  index = i;
  break;
 }
 }

 if (index == -1)
 cout << "error" << endl;
 
 return index;
}

void constructGraph(Graph &G)
{
 cout << "construct graph yooo" << endl;
 cout << "enter vexnum" << endl;
 cin >> ;

 string array[] = {"v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8"};
 const int size = sizeof array / sizeof *array;

 for (int i = 0; i < ; i++)
 {
 [i].name = array[i];
 [i].head = NULL;
 }

 string leftName;
 string rightName;

 cout << "enter a pair" << endl;
 cin >> leftName >> rightName;
 while (leftName != "end" && rightName != "end")
 {
 int leftIndex = findIndex(G, leftName);
 int rightIndex = findIndex(G, rightName);

 Node *node = new Node;
 node->data = rightIndex;
 node->next = NULL;

 node->next = [leftIndex].head;
 [leftIndex].head = node;

 cout << "enter a pair" << endl;
 cin >> leftName >> rightName;
 }
}

bool flag[MAXLEN];

void DFSTranverse(Graph &G, int num)
{
 cout << [num].name << " ";
 flag[num] = true;

 Node *head = [num].head;
 while (head != NULL)
 {
 int index = head->data;
 if (!flag[index])
  DFSTranverse(G, index);
 head = head->next;
 }
}

void main()
{
 Graph G;
 constructGraph(G);
 for (int i = 0; i < MAXLEN; i++)
 flag[i] = false;
 DFSTranverse(G, 0);
}

The iterative traversal algorithm of DFS is as follows:

void DFS(Graph &G)
{
 stack<int> istack;
 (0);

 cout << [0].name << " ";
 flag[0] = true;

 while (!())
 {
 int index = ();
 Node *head = [index].head;

 while (head != NULL && flag[head->data] == true)
  head = head->next;

 if (head != NULL)
 {
  index = head->data;
  if (!flag[index])
  {
  cout << [index].name << " ";
  flag[index] = true;
  (index);
  }
 }
 else
  ();
 }
}

Friends who are sentimental can test and run the example code of this article to deepen their impression. I believe that the description in this article has certain reference value for everyone's C program algorithm design.