在C中用邻接表实现图

时间:2019-04-17 16:31:01

标签: c graph representation

我现在正在学习图形,当我从在线教学资源中了解到用邻接表实现图形时,我对addEdge函数感到困惑。

执行addEdge(graph, 0, 1)时,将创建具有1个值的节点,然后为newNode->next分配graph->array[0].head,该值为空。之后,将graph->array[0].head分配给newNode。然后将节点0和1连接起来,在该函数的下一部分中,它沿相反方向进行。我不知道这种方法如何连接两个节点。

有人可以向我解释吗?

#include <stdio.h> 
#include <stdlib.h> 

// A structure to represent an adjacency list node 
struct AdjListNode 
{ 
    int dest; 
    struct AdjListNode* next; 
}; 

// A structure to represent an adjacency list 
struct AdjList 
{ 
    struct AdjListNode *head;  
}; 

// A structure to represent a graph. A graph 
// is an array of adjacency lists. 
// Size of array will be V (number of vertices  
// in graph) 
struct Graph 
{ 
    int V; 
    struct AdjList* array; 
}; 

// A utility function to create a new adjacency list node 
struct AdjListNode* newAdjListNode(int dest) 
{ 
    struct AdjListNode* newNode = 
     (struct AdjListNode*) malloc(sizeof(struct AdjListNode)); 
    newNode->dest = dest; 
    newNode->next = NULL; 
    return newNode; 
} 

// A utility function that creates a graph of V vertices 
struct Graph* createGraph(int V) 
{ 
    struct Graph* graph =  
        (struct Graph*) malloc(sizeof(struct Graph)); 
    graph->V = V; 

    // Create an array of adjacency lists.  Size of  
    // array will be V 
    graph->array =  
      (struct AdjList*) malloc(V * sizeof(struct AdjList)); 

    // Initialize each adjacency list as empty by  
    // making head as NULL 
    int i; 
    for (i = 0; i < V; ++i) 
        graph->array[i].head = NULL; 

    return graph; 
} 

// Adds an edge to an undirected graph 
void addEdge(struct Graph* graph, int src, int dest) 
{ 
    // Add an edge from src to dest.  A new node is  
    // added to the adjacency list of src.  The node 
    // is added at the begining 
    struct AdjListNode* newNode = newAdjListNode(dest); 
    newNode->next = graph->array[src].head; 
    graph->array[src].head = newNode; 

    // Since graph is undirected, add an edge from 
    // dest to src also 
    newNode = newAdjListNode(src); 
    newNode->next = graph->array[dest].head; 
    graph->array[dest].head = newNode; 
} 

// A utility function to print the adjacency list  
// representation of graph 
void printGraph(struct Graph* graph) 
{ 
    int v; 
    for (v = 0; v < graph->V; ++v) 
    { 
        struct AdjListNode* pCrawl = graph->array[v].head; 
        printf("\n Adjacency list of vertex %d\n head ", v); 
        while (pCrawl) 
        { 
            printf("-> %d", pCrawl->dest); 
            pCrawl = pCrawl->next; 
        } 
        printf("\n"); 
    } 
} 

// Driver program to test above functions 
int main() 
{ 
    // create the graph given in above fugure 
    int V = 5; 
    struct Graph* graph = createGraph(V); 
    addEdge(graph, 0, 1); 
    addEdge(graph, 0, 4); 
    addEdge(graph, 1, 2); 
    addEdge(graph, 1, 3); 
    addEdge(graph, 1, 4); 
    addEdge(graph, 2, 3); 
    addEdge(graph, 3, 4); 

    // print the adjacency list representation of the above graph 
    printGraph(graph); 

    return 0; 
}

1 个答案:

答案 0 :(得分:1)

看起来很简单 您实际上并没有将一个节点链接到另一个节点,您只是在创建一个列表,其中包含此元素已连接到该元素

更多解释:--

打电话

addEdge(graph, 0,1);

那么这个函数就是创建一个名称为(dest)的简单节点

struct AdjListNode* newNode = newAdjListNode(dest);

并且作为节点创建你首先将 newNode 的下一个 ptr 分配给 Source('src') 节点

newNode->next = graph->array[src].head; 

然后你将 SrcNode 分配给 newNode(与我们用来交换元素的方式非常相似)

graph->array[src].head = newNode; 

到目前为止,您已经链接了 Src 和 Dest 节点(但只有一种方式)。

现在从 Dest 到 Src 重复上述过程,使其成为双向路径。

说明 2 :--

addEdge(graph, 0, 1); 

在这一行之后注释一切 并添加

printf(" %d \n",graph->array[0].head->dest);
printf(" %d \n",graph->array[1].head->dest);

首先会输出1 第二个会输出0

即使在 BFS 或 DFS 或其他方式中遍历时,我们也将需要我们实际上不需要连接它们的列表..'

希望你理解!!! 如有错误请指正..????