图的创建和遍历

第一轮在学习数据结构时有意避开了图章节,自我暗示面试不会问。躲还是躲不掉的,学还是要学的。“大话数据结构”结合B站视频学习了一遍。自己将图的邻接矩阵表示方法和邻接表的表示方式以及图的遍历写了一遍,加深印象。

在这里插入图片描述
无向图邻接矩阵表示:
在这里插入图片描述

#include<iostream>
#include<vector>

using namespace std;

struct Graph {
	vector<char> vexs;
	vector<vector<int>> arcs;
	int numberVexs;
	int numberArcs;
};

class Solution {
private:
	int findIndex(vector<char>& s, char c) {
		for (int i = 0; i < s.size(); i++)
			if (s[i] == c) return i;
		return -1;
	}

	vector<bool> used;

	void DFS(Graph* G, int index) {
		used[index] = true;
		cout << G->vexs[index] << " ";
		for (int i = 0; i < G->numberVexs; i++) {
			if (G->arcs[index][i] != INT_MAX && !used[i])
				DFS(G, i);
		}
	}

public:
	void CreateGraph(Graph* G) {
		cout << "Input numberVexs and numberArcs: ";
		cin >> G->numberVexs >> G->numberArcs;
		cout << "Input char for each vexs: ";
		for (int i = 0; i < G->numberVexs; i++) {
			char temp;
			cin >> temp;
			G->vexs.push_back(temp);
		}
		G->arcs = vector<vector<int>>(G->numberVexs, vector<int>(G->numberVexs, INT_MAX));
		cout << "Input start vex and end vex with weight: " << endl;
		char begin, end;
		int weight;
		for (int k = 0; k < G->numberArcs; k++) {
			cin >> begin >> end >> weight;
			int i = findIndex(G->vexs, begin);
			int j = findIndex(G->vexs, end);
			_ASSERT(i >= 0 && j >= 0);
			G->arcs[i][j] = weight;
			G->arcs[j][i] = weight;
		}
	}

	void DFSTraverGraph(Graph* G) {
		used = vector<bool>(G->numberVexs, false);
		for (int i = 0; i < G->numberVexs; i++) {
			if (!used[i])
				DFS(G, i);
		}
	}
};

int main() {
	Solution sol;
	Graph* G = new Graph();
	sol.CreateGraph(G);
	sol.DFSTraverGraph(G);
	return 0;
}

无向图邻接表表示:
在这里插入图片描述

#include<iostream>
#include<vector>

using namespace std;

struct EdgeNode {
	int adjvex;
	int weight;
	EdgeNode* next;
	EdgeNode(int a, int w) {
		adjvex = a;
		weight = w;
		next = nullptr;
	}
};

struct VertexNode {
	char data;
	EdgeNode* firstedge;
};

struct GraphAdjList {
	vector<VertexNode> adjList;
	int numberVertexs;
	int numberEdges;
};

class Solution {
private:
	int findIndex(GraphAdjList* G, char c) {
		for (int i = 0; i < G->numberVertexs; i++)
			if (c == G->adjList[i].data) return i;
		return -1;
	}

	vector<bool> used;

	void DFS(GraphAdjList* G, int index) {
		used[index] = true;
		cout << G->adjList[index].data << " ";
		EdgeNode* ptr = G->adjList[index].firstedge;
		while (ptr) {
			if (!used[ptr->adjvex])
				DFS(G, ptr->adjvex);
			ptr = ptr->next;
		}
	}

public:
	void CreatGraph(GraphAdjList* G) {
		cout << "Input vertexs numbers and edges numbers: ";
		cin >> G->numberVertexs >> G->numberEdges;
		G->adjList = vector<VertexNode>(G->numberVertexs);
		cout << "Input vertexts context: " << endl;
		for (int i = 0; i < G->numberVertexs; i++) {
			cin >> G->adjList[i].data;
			G->adjList[i].firstedge = nullptr;
		}
		cout << "Input edges contex: " << endl;
		for (int k = 0; k < G->numberEdges; k++) {
			char c1, c2;
			int weight;
			cin >> c1 >> c2 >> weight;
			int i = findIndex(G, c1);
			int j = findIndex(G, c2);
			_ASSERT(i >= 0 && j >= 0);

			EdgeNode* temp1 = new EdgeNode(j, weight);
			temp1->next = G->adjList[i].firstedge;
			G->adjList[i].firstedge = temp1;

			EdgeNode* temp2 = new EdgeNode(i, weight);
			temp2->next = G->adjList[j].firstedge;
			G->adjList[j].firstedge = temp2;
		}
	}

	void DFSTraverGraph(GraphAdjList* G) {
		used = vector<bool>(G->numberVertexs, false);
		for (int i = 0; i < G->numberVertexs; i++) {
			if (!used[i])
				DFS(G, i);
		}
	}
};

int main() {
	Solution sol;
	GraphAdjList* G = new GraphAdjList();
	sol.CreatGraph(G);
	sol.DFSTraverGraph(G);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值