图学习笔记

 1、邻接矩阵(vector二维数组)的DFS(递归实现)

class Graph {
public:
    Graph(int vertices);
    void addEdge(int from, int to);
    void DFS(int startVertex);

private:
    int vertices;
    vector> adjMatrix;
    vector visited;

    void DFSRecursive(int vertex);
};

Graph::Graph(int vertices) {
    this->vertices = vertices;
    adjMatrix.resize(vertices, vector(vertices, 0));
    visited.assign(vertices, false);
}

// 无向图
void Graph::addEdge(int from, int to) {
    adjMatrix[from][to] = 1;
    adjMatrix[to][from] = 1; // For undirected graph
}

void Graph::DFS(int startVertex) {
    for (int i = 0; i < vertices; i++) {
        visited[i] = false;
    }

    // 确保每一个点都能被遍历到,包括孤立点
    for (int i = 0; i < vertices; i++) {
        DFSRecursive(startVertex)
    }
}

void Graph::DFSRecursive(int vertex) {
    visited[vertex] = true;
    cout << "Visited vertex: " << vertex << endl;

    for (int i = 0; i < vertices; i++) {
        if (adjMatrix[vertex][i] == 1 && !visited[i]) {
            DFSRecursive(i);
        }
    }
}

  2、邻接表(vector实现)的DFS(栈实现)

class Graph {
public:
    Graph(int vertices);
    void addEdge(int from, int to);
    void DFS(int startVertex);

private:
    int vertices;
    vector> adjList;
};

Graph::Graph(int vertices) {
    this->vertices = vertices;
    adjList.resize(vertices);
}

// 邻接表,每行长度不一。无向图
void Graph::addEdge(int from, int to) {
    adjList[from].push_back(to);
    adjList[to].push_back(from); 
}

void Graph::DFS(int startVertex) {
    vector visited(vertices, false);
    stack s;

    visited[startVertex] = true;
    s.push(startVertex);


    while (!s.empty()) {
        int vertex = s.top();
        s.pop();
        cout << "Visited vertex: " << vertex << endl;

        for (int neighbor : adjList[vertex]) {
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                s.push(neighbor);
            }
        }
    }
}

3、邻接表(vector实现)的BFS(队列实现)

class Graph {
public:
    Graph(int vertices);
    void addEdge(int from, int to);
    void BFS(int startVertex);

private:
    int vertices;
    vector> adjList;
};

Graph::Graph(int vertices) {
    this->vertices = vertices;
    adjList.resize(vertices);
}

// 无向图
void Graph::addEdge(int from, int to) {
    adjList[from].push_back(to);
    adjList[to].push_back(from);
}

void Graph::BFS(int startVertex) {
    vector visited(vertices, false);
    queue q;

    visited[startVertex] = true;
    q.push(startVertex);

    while (!q.empty()) {
        int vertex = q.front();
        q.pop();
        cout << "Visited vertex: " << vertex << endl;

        for (int neighbor : adjList[vertex]) {
            if (!visited[neighbor]) {
                visited[neighbor] = true;
                q.push(neighbor);
            }
        }
    }
}

4、链式前向星DFS和BFS

#include 
#include 
#include 

using namespace std;

const int maxn = 100000; // 假定最大顶点数

struct Edge {
    int to, w, next; // 终点,边权,同起点的上一条边的编号
} edge[maxn]; // 边集

int head[maxn]; // head[i],表示以i为起点的第一条边在边集数组的位置(编号)
int cnt = 0; // 记录边的数量
int n, m; // 顶点数和边数

void init() {
    for (int i = 1; i <= n; i++) head[i] = -1;
    cnt = 0;
}

// 添加新边
void add_edge(int u, int v, int w) {
    edge[cnt].to = v; // 终点
    edge[cnt].w = w; // 权值
    edge[cnt].next = head[u]; // 以u为起点上一条边的编号,也就是与这个边起点相同的上一条边的编号
    head[u] = cnt++; // 更新以u为起点上一条边的编号
}

void DFS(int startVertex) {
    vector visited(n + 1, false);
    stack s;
    
    s.push(startVertex);
    visited[startVertex] = true;
    
    while (!s.empty()) {
        int vertex = s.top();
        s.pop();
        cout << "Visited vertex: " << vertex << endl;
        
        for (int j = head[vertex]; j != -1; j = edge[j].next) {
            int neighborVertex = edge[j].to;
            if (!visited[neighborVertex]) {
                s.push(neighborVertex);
                visited[neighborVertex] = true;
            }
        }
    }
}

void BFS(int startVertex) {
    vector visited(n + 1, false);
    queue q;

    q.push(startVertex);
    visited[startVertex] = true;

    while (!q.empty()) {
        int vertex = q.front();
        q.pop();
        cout << "Visited vertex: " << vertex << endl;

        for (int j = head[vertex]; j != -1; j = edge[j].next) {
            int neighborVertex = edge[j].to;
            if (!visited[neighborVertex]) {
                q.push(neighborVertex);
                visited[neighborVertex] = true;
            }
        }
    }
}

int main() {
    cin >> n >> m;
    int u, v, w;
    init(); // 初始化
    for (int i = 1; i <= m; i++) // 输入m条边
    {
        cin >> u >> v >> w;
        add_edge(u, v, w); // 加边
    }

    int startVertex;
    cout << "Enter the starting vertex for DFS: ";
    cin >> startVertex;
    DFS(startVertex);

    cout << "Enter the starting vertex for BFS: ";
    cin >> startVertex;
    BFS(startVertex);

    return 0;
}

你可能感兴趣的:(数据结构,学习,笔记,算法)