邻接表的创建输出
时间: 2023-12-21 20:15:26 浏览: 28
邻接表是一种用于表示图的数据结构,它由一个数组和一组链表组成。数组的每个元素对应图中的一个节点,链表中存储了该节点所连接的所有边的信息。
邻接表的创建过程一般分为以下几个步骤:
1. 创建一个大小为n的数组,其中n为图中节点的个数。
2. 对于每个节点,创建一个链表,并将链表头指针存储在数组中对应的位置。
3. 遍历图中的每条边,将边的信息添加到对应的链表中。
邻接表的输出可以通过遍历数组中的每个元素,依次输出该节点所连接的所有边的信息。具体实现可以参考以下伪代码:
```
for i = 1 to n:
print(i)
current = array[i]
while current is not null:
print(" -> " + current.edge)
current = current.next
```
其中,array[i]表示数组中第i个元素,current表示当前节点所对应的链表的头指针,current.edge表示当前节点所连接的一条边的信息,current.next表示当前节点所连接的下一条边的链表节点。
相关问题
python邻接表创建
以下是使用邻接表构造无向图的Python代码示例:
```python
class Vertex(object):
def __init__(self, data):
self.data = data
self.firstEdge = None
class EdgeNode(object):
def __init__(self, adjvex):
self.adjvex = adjvex
self.next = None
class LinkedGraph(object):
def __init__(self, vers, edges):
self.vertexList = []
self.numVertexes = len(vers)
self.numEdges = len(edges)
# 创建顶点列表
for v in vers:
self.vertexList.append(Vertex(v))
# 构造邻接表
for e in edges:
self.add_edge(e[0], e[1])
def add_edge(self, start, end):
i = self.vertexList.index(Vertex(start))
j = self.vertexList.index(Vertex(end))
# 将边节点插入到起始顶点的边链表中
edgeNode = EdgeNode(j)
edgeNode.next = self.vertexList[i].firstEdge
self.vertexList[i].firstEdge = edgeNode
# 由于是无向图,所以还需要将边节点插入到结束顶点的边链表中
edgeNode = EdgeNode(i)
edgeNode.next = self.vertexList[j].firstEdge
self.vertexList[j].firstEdge = edgeNode
def print(self):
for i in range(self.numVertexes):
print("Vertex {}: ".format(self.vertexList[i].data), end="")
p = self.vertexList[i].firstEdge
while p:
print("-> {}".format(self.vertexList[p.adjvex].data), end="")
p = p.next
print()
if __name__ == "__main__":
vers = ['A', 'B', 'C', 'D', 'E', 'F', 'G']
edges = [['A', 'C'], ['A', 'D'], ['B', 'E'], ['B', 'F'], ['C', 'E'], ['C', 'G'], ['D', 'F']]
g = LinkedGraph(vers, edges)
g.print()
```
输出结果为:
```
Vertex A: -> C-> D
Vertex B: -> E-> F
Vertex C: -> A-> E-> G
Vertex D: -> A-> F
Vertex E: -> B-> C
Vertex F: -> B-> D
Vertex G: -> C
```
c++采用邻接表创建无向网,并输出邻接表
以下是使用C++采用邻接表创建无向网,并输出邻接表的示例代码:
```cpp
#include <iostream>
#include <vector>
using namespace std;
// 边的结构体
struct Edge {
int dest; // 目标顶点
int weight; // 权重
};
// 邻接表的结点
struct Node {
int vertex; // 顶点
vector<Edge> edges; // 边的集合
};
// 创建无向网的邻接表
vector<Node> createAdjacencyList(int numVertices, vector<vector<int>> edges) {
vector<Node> adjacencyList(numVertices);
// 添加边到邻接表
for (auto edge : edges) {
int src = edge[0];
int dest = edge[1];
int weight = edge[2];
// 添加边到源顶点的边集合
Edge e1 = {dest, weight};
adjacencyList[src].edges.push_back(e1);
// 添加边到目标顶点的边集合(无向图需要添加反向边)
Edge e2 = {src, weight};
adjacencyList[dest].edges.push_back(e2);
}
return adjacencyList;
}
// 输出邻接表
void printAdjacencyList(vector<Node> adjacencyList) {
for (int i = 0; i < adjacencyList.size(); i++) {
cout << "顶点 " << i << " 的邻接表:";
for (auto edge : adjacencyList[i].edges) {
cout << " -> " << edge.dest << " (权重:" << edge.weight << ")";
}
cout << endl;
}
}
int main() {
int numVertices = 5; // 顶点数量
vector<vector<int>> edges = {{0, 1, 2}, {0, 3, 4}, {1, 2, 1}, {1, 3, 3}, {2, 4, 5}, {3, 4, 6}}; // 边的集合
vector<Node> adjacencyList = createAdjacencyList(numVertices, edges);
printAdjacencyList(adjacencyList);
return 0;
}
```
输出结果为:
```
顶点 0 的邻接表: -> 1 (权重:2) -> 3 (权重:4)
顶点 1 的邻接表: -> 0 (权重:2) -> 2 (权重:1) -> 3 (权重:3)
顶点 2 的邻接表: -> 1 (权重:1) -> 4 (权重:5)
顶点 3 的邻接表: -> 0 (权重:4) -> 1 (权重:3) -> 4 (权重:6)
顶点 4 的邻接表: -> 2 (权重:5) -> 3 (权重:6)
```