Share c++ đồ thị là gì

quangtruong296

New member
Trong C ++ ## biểu đồ trong C ++ là gì?

Biểu đồ là cấu trúc dữ liệu đại diện cho một tập hợp các đối tượng (được gọi là các đỉnh) và các mối quan hệ giữa chúng (được gọi là các cạnh).Trong C ++, một biểu đồ có thể được biểu diễn bằng danh sách kề hoặc ma trận kề.

Danh sách kề là một danh sách các danh sách, trong đó mỗi danh sách đại diện cho các đỉnh liền kề với một đỉnh nhất định.Ví dụ: danh sách kề sau đây thể hiện biểu đồ với 4 đỉnh:

`` `
0 -> [1, 2]
1 -> [0, 2, 3]
2 -> [0, 1, 3]
3 -> [1, 2]
`` `

Trong biểu đồ này, đỉnh 0 liền kề với các đỉnh 1 và 2, đỉnh 1 liền kề với các đỉnh 0, 2 và 3, v.v.

Một ma trận kề là một ma trận vuông, trong đó mỗi phần tử biểu thị trọng lượng của cạnh giữa hai đỉnh.Ví dụ: ma trận kề sau đây biểu thị cùng một biểu đồ với danh sách kề trên:

`` `
0 1 2 3
0 0 1 1
1 1 0 1
2 1 1 0
3 1 1 0
`` `

Trong ma trận này, phần tử ở hàng 0 và cột 1 là 1, có nghĩa là có một cạnh giữa đỉnh 0 và đỉnh 1 với trọng lượng là 1.

Đồ thị được sử dụng trong một loạt các ứng dụng, chẳng hạn như:

* Mạng xã hội
* Thuật toán định tuyến
* Học máy
* Tầm nhìn máy tính

## Cách tạo biểu đồ trong C ++

Để tạo biểu đồ trong C ++, bạn có thể sử dụng lớp `std :: vector` để lưu trữ các đỉnh và lớp` std :: unetered_map` để lưu trữ các cạnh.

Mã sau đây tạo ra một biểu đồ với 4 đỉnh và 3 cạnh:

`` `C ++
#include <Istream>
#include <Vector>
#include <Nemedered_map>

sử dụng không gian tên STD;

// một đỉnh trong biểu đồ.
struct vertex {
ID int;
Vector <Int> AdjacentVerse;
};

// một cạnh trong biểu đồ.
Struct Edge {
int SourceverTex;
Int DestinationverTex;
trọng lượng int;
};

// Một đồ thị.
Biểu đồ cấu trúc {
vector <ertex> đỉnh;
Vector <Gart> cạnh;
};

// Tạo biểu đồ với 4 đỉnh và 3 cạnh.
Đồ thị creategraph () {
// Tạo các đỉnh.
Đỉnh v1 = {0, {1, 2}};
Đỉnh v2 = {1, {0, 2, 3}};
Đỉnh v3 = {2, {0, 1, 3}};
Đỉnh v4 = {3, {1, 2}};

// Tạo các cạnh.
Cạnh e1 = {0, 1, 1};
Cạnh e2 = {1, 2, 1};
Cạnh e3 = {2, 3, 1};

// Tạo biểu đồ.
Đồ thị đồ thị = {
{v1, v2, v3, v4},
{E1, E2, E3}
};

trả lại đồ thị;
}

// In biểu đồ.
void printGraph (biểu đồ biểu đồ) {
for (int i = 0; i <graph.vertices.size (); i ++) {
cout << "đỉnh" << Graph.vertices .id << ":";
for (int j = 0; j <graph.vertices .adjacentvertices.size (); j ++) {
cout << Graph.vertices .adjacentvertices [j] << "";
}
cout << endl;
}
}

int main () {
// Tạo một biểu đồ.
Đồ thị đồ thị = createdEgraph ();

// In biểu đồ.
bản in
=======================================
in C++ ## What is a graph in C++?

A graph is a data structure that represents a set of objects (called vertices) and the relationships between them (called edges). In C++, a graph can be represented using an adjacency list or an adjacency matrix.

An adjacency list is a list of lists, where each list represents the vertices adjacent to a given vertex. For example, the following adjacency list represents a graph with 4 vertices:

```
0 -> [1, 2]
1 -> [0, 2, 3]
2 -> [0, 1, 3]
3 -> [1, 2]
```

In this graph, vertex 0 is adjacent to vertices 1 and 2, vertex 1 is adjacent to vertices 0, 2, and 3, and so on.

An adjacency matrix is a square matrix, where each element represents the weight of the edge between two vertices. For example, the following adjacency matrix represents the same graph as the adjacency list above:

```
0 1 2 3
0 0 1 1
1 1 0 1
2 1 1 0
3 1 1 0
```

In this matrix, the element at row 0 and column 1 is 1, which means that there is an edge between vertex 0 and vertex 1 with a weight of 1.

Graphs are used in a variety of applications, such as:

* Social networking
* Routing algorithms
* Machine learning
* Computer vision

## How to create a graph in C++

To create a graph in C++, you can use the `std::vector` class to store the vertices and the `std::unordered_map` class to store the edges.

The following code creates a graph with 4 vertices and 3 edges:

```c++
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

// A vertex in the graph.
struct Vertex {
int id;
vector<int> adjacentVertices;
};

// An edge in the graph.
struct Edge {
int sourceVertex;
int destinationVertex;
int weight;
};

// A graph.
struct Graph {
vector<Vertex> vertices;
vector<Edge> edges;
};

// Create a graph with 4 vertices and 3 edges.
Graph createGraph() {
// Create the vertices.
Vertex v1 = {0, {1, 2}};
Vertex v2 = {1, {0, 2, 3}};
Vertex v3 = {2, {0, 1, 3}};
Vertex v4 = {3, {1, 2}};

// Create the edges.
Edge e1 = {0, 1, 1};
Edge e2 = {1, 2, 1};
Edge e3 = {2, 3, 1};

// Create the graph.
Graph graph = {
{v1, v2, v3, v4},
{e1, e2, e3}
};

return graph;
}

// Print the graph.
void printGraph(Graph graph) {
for (int i = 0; i < graph.vertices.size(); i++) {
cout << "Vertex " << graph.vertices.id << ": ";
for (int j = 0; j < graph.vertices.adjacentVertices.size(); j++) {
cout << graph.vertices.adjacentVertices[j] << " ";
}
cout << endl;
}
}

int main() {
// Create a graph.
Graph graph = createGraph();

// Print the graph.
printGraph
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top