C实现深度优先搜索算法,不使用指针(以邻接矩阵表示图)

huangapple go评论60阅读模式
英文:

C Implementation of Depth First Search Algorithm Without Pointers (Graph as Adjacency Matrix)

问题

以下是代码的部分翻译:

#include  /* 包括 printf 的头文件 */
#define maxV 100 /* 定义一个常数 */

int V, E, x, y; /* 声明变量 */
int a[maxV][maxV]; /* 声明一个二维数组 */
int b[maxV][maxV];
int count = 0; /* 声明并初始化变量 */
void read_graph(void); /* 函数原型 */
void print_graph(void);
void print_graph_grid(void);
void copy_array(void);

void main() {/* 主程序。 */
    read_graph(); /* 调用函数输入图的邻接矩阵 */
    //print_graph(); /* 调用函数输出图的邻接矩阵 */
    print_graph_grid();
    copy_array();
 }
void read_graph( void ) {/* 读取图的邻接矩阵的函数 */

    int edge, x;
    printf("请输入顶点数:");
    scanf("%d", &V);

    if (V > maxV)
        printf("超出允许的最大顶点数");

    else {
        for (x=1; x <= V; x++)
            for (y=1; y <= V; y++)
                a[x][y] = 0;
        for (x=1; x <= V; x++)
            for (y=x; y <= V; y++) {
                printf("\na[ %d ][ %d ]=", x, y);
                scanf("%d", &edge);
                a[x][y] = edge;
                a[y][x] = edge;
            }
        }
    }
void print_graph(void)  {/* 打印图的邻接矩阵的函数 */

    int x,y;
    for (x=1; x <= V; x++)
        for (y=1; y <= V; y++)
            printf("\na[ %d ][ %d ]= %d", x, y, a[x][y]);
    }

void print_graph_grid(void) {
     printf("\n图的邻接矩阵:\n");
      for(int i=1; i<=V; i++) {
            printf("%d (", i);
        for(int j=1; j<=V; j++) {
          printf("%d ", a[i][j]);
        }
        printf(")");
    printf("\n");
        }
    }

void copy_array(void) {
    for(int i=1; i<=V; i++) {
        for(int j=1; j<=V; j++) {
                if (b[i][j] == a[i][j]) {

                }
            }
        }
}

void dfs(void) {
    int component;
    for(int i=1; i<=V; i++) {
        for(int j=1; j<=V; j++) {
                if (b[i][j] == 0) {
                    
                }
            }
        }
    }

请注意,这只是代码的部分翻译,不包括问题描述部分。

英文:

The Question can be seen in the image:
Question

The following is the sample run info for the code:
Test info for the Question

Please refer to the links above for better understanding of the question.

I looked at some solution using pointers but I don't really understand, would it be possible to write the solution without pointers and only arrays.

<pre><code>
#include /* Include header file for printf /
#define maxV 100 /
Define a constant */

int V, E, x, y; /* Declare variables /
int a[maxV][maxV]; /
Declare a two dimensional array /
int b[maxV][maxV]
int count = 0; /
Declare and initialize variable /
void read_graph(void); /
Function prototype */
void print_graph(void);
void print_graph_grid(void);
void copy_array(void);

void main() {/* Main program. /
read_graph(); /
call function to input graph adjacency matrix /
//print_graph(); /
call function to output graph adjacency matrix /
print_graph_grid();
> copy_array();
}
void read_graph( void ) {/
Function to read graph adjacency matrix */

int edge, x;
printf(&quot;\nInput number of vertices :&quot;);
scanf(&quot;%d&quot;, &amp;V);
if (V &gt; maxV)
printf(&quot;Exceed the maximum number of vertices permitted&quot;);
else {
for (x=1; x &lt;= V; x++)
for (y=1; y &lt;= V; y++)
a[x][y] = 0;
for (x=1; x &lt;= V; x++)
for (y=x; y &lt;= V; y++) {
printf(&quot;\na[ %d ][ %d ]=&quot;, x, y);
scanf(&quot;%d&quot;, &amp;edge);
a[x][y] = edge;
a[y][x] = edge;
}
}
}

void print_graph(void) {/* Function to print graph adjacency matrix */

int x,y;
for (x=1; x &lt;= V; x++)
for (y=1; y &lt;= V; y++)
printf(&quot;\na[ %d ][ %d ]= %d&quot;, x, y, a[x][y]);
}

void print_graph_grid(void) {
printf("\nGraph Adjacency Matrix:\n");
for(int i=1; i<=V; i++) {
printf("%d ( 4", i);
for(int j=1; j<=V; j++) {
printf("%d ", a[i][j]);
}
printf(")");
printf("\n");
}
}

void copy_array(void) {
for(int i=1; i<=V; i++) {
for(int j=1; j<=V; j++) {
if (b[i][j] == a[i][j]) {

            }
}
}

}

void dfs(void) {
int component;
for(int i=1; i<=V; i++) {
for(int j=1; j<=V; j++) {
if (b[i][j] == 0) {

            }
}
}
}

</code></pre>

答案1

得分: 1

**EDIT**: graph[7][7], visited[7] set as global variables after ravenspoint's comments.

#include <stdio.h>

int n = 7;
int graph[7][7], visited[7];

void DFS(int i)
{
    visited[i] = 1;
    for (int j = 0; j < n; j++)
        if (visited[j] == 0 && graph[i][j] == 1)
        {
            visited[j] = 1;
            printf(" %d", j);
            DFS(j);
        }
}

int main()
{

    for (int i = 0; i < n; i++)
    {
        visited[i] = 0;
        for (int j = 0; j < n; j++)
            graph[i][j] = 0;
    }
    graph[0][1] = graph[0][2] = graph[1][0] = graph[1][2] = graph[2][0] = graph[2][1] = graph[2][3] = graph[3][2] = graph[4][5] = graph[4][6] = graph[5][4] = graph[5][6] = graph[6][4] = graph[4][5] = 1;

    printf("%d:", 0);
    DFS(0);
    printf("\n\n");
    return 0;
}

n是节点数量(从0到6标记),graph[i][j] 是根据您提供的链接的邻接矩阵。这里以节点0作为根节点(或起点)进行应用:

DFS(0);

您可以选择任何节点作为根节点,从0到6。

结果是 0: 1 2 3,表示从节点0可以到达节点1、2和3,而无法到达节点4、5和6。

英文:

EDIT: graph[7][7], visited[7] set as global variables after ravenspoint's comments.

#include&lt;stdio.h&gt;
int n = 7;
int graph[7][7], visited[7];
void DFS(int i)
{
visited[i]=1;
for(int j=0;j&lt;n;j++)
if(visited[j] == 0 &amp;&amp; graph[i][j] == 1 )  {
visited[j] = 1;
printf(&quot; %d&quot;, j);
DFS(j);
}
}
int main()  {
for(int i=0;i&lt;n;i++)  {
visited[i]=0;
for(int j=0;j&lt;n;j++)  graph[i][j] = 0;
}
graph[0][1] = graph[0][2] = graph[1][0] = graph[1][2] = graph[2][0] = graph[2][1] = graph[2][3] = graph[3][2] = graph[4][5] = graph[4][6] = graph[5][4] = graph[5][6] = graph[6][4] = graph[4][5] = 1;
printf(&quot;%d:&quot;, 0);
DFS(0);
printf(&quot;\n\n&quot;);
return 0;
}

n is the number of nodes (labeled from 0 to 6) and graph[i][j] is the adjacency matrix according to the link you provided. Here, it is applied for node 0 as the root (or origin):

DFS(0);

You can put any node as root, from 0 to 6.

The result is 0: 1 2 3, meaning that nodes 1,2,3 are reachable from node 0, and nodes 4,5,6 are not.

答案2

得分: 0

将邻接矩阵设为全局变量将允许你处理包含数千个节点的图。

#include <stdio.h>
#define MAX_NODES 1000
int n = 7;
int global_visited[MAX_NODES];
int global_adjacency[MAX_NODES][MAX_NODES];

void DFS(int i)
{
    global_visited[i] = 1;
    for (int j = 0; j < n; j++)
        if (global_visited[j] == 0 && global_adjacency[i][j] == 1) {
            global_visited[j] = 1;
            printf(" %d", j);
            DFS(j);
        }
}

int main() {

    if (n > MAX_NODES) {
        printf("MAX Nodes exceeded\n");
        return 1;
    }
    for (int i = 0; i < n; i++) {
        global_visited[i] = 0;
        for (int j = 0; j < n; j++) {
            global_adjacency[i][j] = 0;
        }
    }

    global_adjacency[0][1] = global_adjacency[0][2] = global_adjacency[1][0]
        = global_adjacency[1][2] = global_adjacency[2][0] = global_adjacency[2][1]
        = global_adjacency[2][3] = global_adjacency[3][2] = global_adjacency[4][5]
        = global_adjacency[4][6] = global_adjacency[5][4] = global_adjacency[5][6]
        = global_adjacency[6][4] = global_adjacency[4][5] = 1;

    printf("%d:", 0);
    DFS(0);
    printf("\n\n");
    return 0;
}
英文:

Make the adjacency matrix global will allow you to handle graphs containing thousands of nodes

#include&lt;stdio.h&gt;
#define MAX_NODES 1000
int n = 7;
int global_visited[MAX_NODES];
int global_adjacency[MAX_NODES][MAX_NODES];
void DFS(int i)
{
global_visited[i]=1;
for(int j=0;j&lt;n;j++)
if(global_visited[j] == 0 &amp;&amp; global_adjacency[i][j] == 1 )  {
global_visited[j] = 1;
printf(&quot; %d&quot;, j);
DFS(j);
}
}
int main()  {
if( n &gt; MAX_NODES ) {
printf(&quot;MAX Nodes exceeded\n&quot;);
return 1;
}
for(int i=0;i&lt;n;i++)  {
global_visited[i]=0;
for(int j=0;j&lt;n;j++)  {
global_adjacency[i][j] = 0;
}
}
global_adjacency[0][1] = global_adjacency[0][2] = global_adjacency[1][0]
= global_adjacency[1][2] = global_adjacency[2][0] = global_adjacency[2][1]
= global_adjacency[2][3] = global_adjacency[3][2] = global_adjacency[4][5] 
= global_adjacency[4][6] = global_adjacency[5][4] = global_adjacency[5][6] 
= global_adjacency[6][4] = global_adjacency[4][5] = 1;
printf(&quot;%d:&quot;, 0);
DFS(0);
printf(&quot;\n\n&quot;);
return 0;
}

huangapple
  • 本文由 发表于 2023年3月8日 16:05:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75670589.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定