图的邻接(链)表表示法 Graph adjacency list representation

邻接

邻接点: 对于无向图G=(V,{E}),若边(v,v’)∈E,则v和v’互为邻接点

简单描述:两个顶点之间有边相连就叫做邻接。

无权图的邻接表表示(一)

An adjacency list represents a graph as an array of linked lists. The index of the array represents a vertex and each element in its linked list represents the other vertices that form an edge with the vertex.

data structures

struct node{
    int vertex;
    struct node* next;
};

struct Graph{
    int numVertices;
    struct node** adjLists;
};

带权重图的邻接表

https://www.cnblogs.com/Camilo/p/3923468.html
https://blog.csdn.net/weixin_34005042/article/details/85980677

在这里插入图片描述
图中
顶点表:数组,支持随机存取。索引为顶点 编号。邻接(链)表中又称作表头。
边表:里面的数字是与顶点相邻的顶点编号(从0开始,与数组索引对应)。它是每一个链中的除表头部分——图中箭头后面的部分。
以第0个顶点为例,v0与编号/索引为1、2、3 的顶点相邻,通过查数组可得顶点名称——v1、v2、v3。

数据结构: https://blog.csdn.net/ASCIIdragon/article/details/84635236

//边
typedef struct ArcNode{
	int Adjvex;  // 这个索引值是边表节点自己在数组中的索引值——表头中定点的索引值。以第0个顶点为例,它的值是1、2或者3。
	ArcNode* nextarc;   //指向下一个邻接点。  用于串连所有与表头某一节点相邻的所有顶点。
	int weight;  // 边的权重。 无权图无此项
}ArcNode;

//顶点
typedef struct VNode{
	char  data;              // 如: v1,  v2  
	ArcNode* firstarc;       //与该顶点相连的所有的边 。实际存储的是与表头中某一顶点相邻的顶点和顶点之间*“**边**”*的***权重***
}AdjList[MAX_VERTEX_NUM];

typedef struct {
    int V_num;
    adjList    VerticesList;			//  arr
} Graph;
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
邻接表是一种表示方法,它使用来存储每个节点的邻居节点。在C语言中,可以使用结构体和指针来实现邻接表。以下是一个示例代码: ``` #include <stdio.h> #include <stdlib.h> // 邻接表节点结构体 typedef struct AdjListNode { int dest; struct AdjListNode* next; } AdjListNode; // 邻接表头结构体 typedef struct AdjList { AdjListNode* head; } AdjList; // 结构体 typedef struct Graph { int V; AdjList* array; } Graph; // 创建邻接表节点 AdjListNode* newAdjListNode(int dest) { AdjListNode* newNode = (AdjListNode*)malloc(sizeof(AdjListNode)); newNode->dest = dest; newNode->next = NULL; return newNode; } // 创建 Graph* createGraph(int V) { Graph* graph = (Graph*)malloc(sizeof(Graph)); graph->V = V; graph->array = (AdjList*)malloc(V * sizeof(AdjList)); for (int i = 0; i < V; i++) { graph->array[i].head = NULL; } return graph; } // 添加边 void addEdge(Graph* graph, int src, int dest) { AdjListNode* newNode = newAdjListNode(dest); newNode->next = graph->array[src].head; graph->array[src].head = newNode; newNode = newAdjListNode(src); newNode->next = graph->array[dest].head; graph->array[dest].head = newNode; } // 打印邻接表 void printGraph(Graph* graph) { for (int i = 0; i < graph->V; i++) { AdjListNode* node = graph->array[i].head; printf("邻接表节点 %d 的邻居节点:", i); while (node) { printf("%d ", node->dest); node = node->next; } printf("\n"); } } int main() { Graph* graph = createGraph(5); 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); printGraph(graph); return 0; } ``` 这段代码实现了一个无向邻接表表示法,可以通过调用 `createGraph` 函数创建一个具有 `V` 个节点的,然后通过 `addEdge` 函数添加边,最后通过 `printGraph` 函数打印邻接表

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值