简体   繁体   English

从图表开始

[英]Starting with graphs

I know this may sound a lot naive, but can someone please explain me how can i implement graphs in C language. 我知道这可能听起来很天真,但有人可以解释一下我如何用C语言实现图形。 I have read the theory, but I am not able to get off the blocks with graph programming. 我已经读过这个理论,但是我无法通过图形编程来解决问题。


I would really appreciate if someone could explain how would to create a graph using adjacency lists and adjacency matrix and how would you perform breadth first search and depth first search in C code with some explanations 如果有人能够解释如何使用邻接列表和邻接矩阵创建图形,我将非常感激,您将如何在C代码中执行广度优先搜索和深度优先搜索以及一些解释


And before anything, I would like to tell you that this is not a homework. 在此之前,我想告诉你,这不是一个功课。 I really want to learn graphs but can't afford a tutor. 我真的想学习图表但却买不起导师。

I assume that here graph is a collection of vertex and edges. 我假设这里的图是顶点和边的集合。 For that you would need an array of pointer to structures. 为此,您需要一个指向结构的指针数组。 This is adjacency list representation of graph. 这是图的邻接列表表示。 These structures would having at least an value, which is node number and pointer to another structure. 这些结构至少具有一个值,即节点号和指向另一个结构的指针。 While inserting a new node to graph just go to appropriate index of array and push the node at beginning. 在图形中插入新节点时,只需转到相应的数组索引并在开始时推送节点。 This is O(1) time for insertion. 这是O(1)插入时间。 My implementation might help you in understanding how it really works. 我的实现可能会帮助您了解它是如何工作的。 If you are having good skills at C this wouldn't take much longer to understand the code. 如果你在C语言上有很好的技能,那么理解代码就不会花费太长时间。

//  Graph implementation by adjacency list

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

#define MAX_SIZE 1000

typedef struct node{
    int number;
    struct node * next;
} Node;


//  U is starting node, V is ending node
void addNode (Node *G[], int U, int V, int is_directed)
{
    Node * newnode = (Node *)malloc(sizeof(Node));
    newnode->number = V;
    newnode->next = G[U];
    G[U] = newnode;

//  0 for directed, 1 for undirected
    if (is_directed)
    {
        Node * newnode = (Node *)malloc(sizeof(Node));
        newnode->number = U;
        newnode->next = G[V];
        G[V] = newnode;
    }
}

void printgraph(Node *G[], int num_nodes)
{
    int I;
    for (I=0; I<=num_nodes; I++)
    {
        Node *dum = G[I];
        printf("%d : ",I);
        while (dum != NULL)
        {
            printf("%d, ",dum->number);
            dum =dum->next;
        }
        printf("\n");
    }

}



void dfs (Node *G[], int num_nodes, int start_node)
{
    int stack[MAX_SIZE];
    int color[num_nodes+1];
    memset (color, 0, sizeof(color));
    int top = -1;
    stack[top+1] = start_node;
    top++;
    while (top != -1)
    {
        int current = stack[top];
        printf("%d  ",current);
        top--;
        Node *tmp = G[current];
        while (tmp != NULL)
        {
            if (color[tmp->number] == 0)
            {
                stack[top+1] = tmp->number;
                top++;
                color[tmp->number] = 1;
            }
            tmp = tmp->next;
        }
    }

}

void bfs (Node *G[], int num_nodes, int start_node)
{
    int queue[MAX_SIZE];
    int color[num_nodes+1];
    memset (color, 0, sizeof (color));
    int front=-1, rear=-1;
    queue[rear+1] = start_node;
    rear++;printf("\n\n");
    while (front != rear)
    {
        front++;
        int current = queue[front];
        printf("%d  ",current);

        Node *tmp = G[current];
        while (tmp != NULL)
        {
            if (color[tmp->number] == 0)
            {
                queue[rear+1] = tmp->number;
                rear++;
                color[tmp->number] = 1;
            }
            tmp = tmp->next;
        }
    }

}  

int main(int argc, char **argv)
{
    int num_nodes;
    // For Demo take num_nodes = 4
    scanf("%d",&num_nodes);
    Node *G[num_nodes+1];
    int I;
    for (I=0; I<num_nodes+1 ;I++ )
        G[I] = NULL;

    addNode (G, 0, 2, 0);
    addNode (G, 0, 1, 0);
    addNode (G, 1, 3, 0);
    addNode (G, 2, 4, 0);
    addNode (G, 2, 1, 0);
    printgraph( G, num_nodes);
    printf("DFS on graph\n");
    dfs(G, num_nodes, 0);
    printf("\n\nBFS on graph\n");
    bfs(G, num_nodes, 0);

    return 0;
} 

Well, a real naive and basic answer would be that graph can be represented in C using data structures that contain their pointers to other such data structures. 好吧,一个真正天真和基本的答案是,图表可以使用包含指向其他此类数据结构的指针的数据结构在C中表示。 Graphs are really just doubly linked lists that can have multiple links from a single node. 图表实际上只是双链表,可以从单个节点拥有多个链接。 If you haven't digested linked lists and doubly linked lists, that'd be a good place to start. 如果你还没有消化链表和双链表,那将是一个很好的起点。

So let's say you have a adjacency list, {a,b},{b,c},{d},{b,e}. 因此,假设您有一个邻接列表{a,b},{b,c},{d},{b,e}。 First off, you parse that and make a list of all your unique items. 首先,您解析它并列出所有独特的项目。 (A regular linked list, array, whatever, it's just a temporary structure to help you. You could bypass that, do it on the fly, and probably reap a speedup, but this is simple.) Walking through that list, you generate a node for each item. (一个常规的链表,数组,无论如何,它只是一个临时的结构来帮助你。你可以绕过它,动态地做,并且可能获得加速,但这很简单。)通过该列表,你生成一个每个项目的节点。 For each node, you go through the adjacency list again and create an edge when it sees itself. 对于每个节点,您将再次浏览邻接列表,并在看到自身时创建边缘。 This is a pointer inside the node pointing to another node. 这是指向另一个节点的节点内的指针。

In the end you have a regular list of all you nodes, so you don't lose that lone 'd' node hanging out by itself. 最后,您有一个包含所有节点的常规列表,因此您不会丢失单独的“d”节点。 You also have a graph of all your nodes so you know their relationship to each other. 您还有一个所有节点的图表,以便您了解它们之间的关系。

Search 搜索
Searching across graphs is a pretty basic idea. 搜索图表是一个非常基本的想法。 Start in a node, compare, move to one of it's neighbors and do it again. 从节点开始,比较,移动到其中一个邻居并再次执行。 There are a lot of pitfalls though. 但是有很多陷阱。 Like getting into an endless loop and knowing when to stop. 就像进入一个无限循环,知道何时停止。

You'll have to ask more specific questions if you want a better explanation than what you can find online already. 如果你想要一个比你在网上找到的更好的解释,你将不得不提出更具体的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM