浅学C++(5)数据结构与算法(数组、矩阵、图)

数组与矩阵:
    数组:存储空间连续的表结构
    矩阵:带二维信息的数据,一般用二维数组来存储矩阵
    特殊矩阵:
        稀疏矩阵:有效的信息数据不多,绝大多数都是无效信息数据不需要存储,没有特定的标准,全凭感觉
        这些矩阵如果使用二维数组来存储的话,会非常浪费存储空间,为了节约空间,我们可以对这些矩阵进行压缩
            普通稀疏矩阵压缩方式:
                采用三元组进行压缩
                三元组:有三个数据项:行、列、值 构成一个新整体,既可以顺序存储也可以链式存储三元组数据

        注意:稀疏矩阵压缩后就丢失了随机访问的功能 
        
        上三角形矩阵:n行n列  第i行  第j列
            [0][1][3][6]
            [] [2][4][7]
            [] [] [5][8]
            [] [] [] [9]
            压缩方式:使用一维数组进行存储
            一维数组长度:n*(n+1)/2
            对应关系: (j+1)*j/2+i
            数据位置:i<=j
        下三角形矩阵:
            [0] []  []  []
            [1] [2] []  []
            [3] [4] [5] []
            [6] [7] [8] [9]
            压缩方式:使用一维数组进行存储
            一维数组长度:n*(n+1)/2
            对应关系: (i+1)*i/2+j
            数据位置:i>=j

        对称矩阵:
            沿着对角线(0,0)(1,1)... 两边数据对称
            [0] [1] [3] [6]
            [1] [2] [4] [7]
            [3] [4] [5] [8]
            [6] [7] [8] [9]
            压缩方式:使用一维数组进行存储,把它当做上三角或者下三角进行压缩即可
            一维数组长度:n*(n+1)/2
            对应关系: (i+1)*i/2+j
            i和j要满足:当i>j 除了还原(i,j) 还要还原(j,i)

        对角\对状矩阵:
            沿着对角线(0,0)(1,1)... 旁边的两边都有数据
            [0] [1] [ ] [ ]
            [2] [3] [4] [ ]
            [ ] [5] [6] [7]
            [ ] [ ] [8] [9]
            压缩方式:使用一维数组进行存储
            数组长度:3*n-2
            对应关系:i*2+j
            i和j要满足:abs(i-j) <= 1 的位置有值

图(Graph)型结构:
    什么是图型结构:由有穷且非空的顶点和顶点之间边的集合组成
    通常表示: G(V,E)  G表示一个图,V是图中顶点集合(元素),E是图中所有边(元素之间的关系)的集合

    无向图:
        边用(A,B)方式表示,A与B之间互通,边没有指定方向
    无向完全图:
        在无向图中,任意两个顶点之间都有边,这种叫做无向完全图
        含有n个顶点的无向完全图中,共有 n*(n-1)/2 条边
    有向图:
        边用<A,B>方式表示,仅仅表示从A点到B点的边,有向图中边也叫做弧,A是弧尾,B是弧头
    有向完全图:
        在有向图中,任意两个顶点之间都有方向相反的两条弧,这种图叫做有向完全图
        含有n个顶点的有向完全图中,共有 n*(n-1) 条边

    注意:不讨论顶点到自身的边,且不讨论重复的边,这种图统称为简单图,数据结构中只研究简单图

    稀疏图:顶点多边少的图,反之称为稠密图
    边的权重:图中的边附带有意义的数据,这些数据叫做边的权重,带权重的图也称为网
    度:依附于顶点的边的数量称之为该顶点的度,有向图中,度分为出度(从该顶点出发的弧的数量)、入度(指向该顶点的弧的数量)
    路径:顶点到顶点之间经过的边叫做路径
    路径长度:路径上边的条目数
    环:图中某个点通过边最后能绕回该点
    回路:专指有向图,从某点出发,最终有弧回到该点,如果某点只有输出或输入,该点没有回路
    简单路径:边经过顶点序列中不重复的路径称为简单路径
    简单回路:除了第一个顶点和最后一个顶点外,其余顶点不重复的回路称为简单回路
    连通:如果顶点V到顶点V1之间有路径,则称V和V1是连通的
    连通图:任意顶点之间都是连通的,称之为连通图,如果一个图中有n个顶点则至少需要 n-1 条边才能达到连通图
    生成树:顶点数为n,仅需要n-1条边的连通图,称之为生成树,如果给边配上权重,权重和最小的生成树称之为最小生成树

图的存储结构:
    邻接矩阵:
        用一个一维数组存储n个顶点,用一个n*n的二维数组存储边
        char V[n] = {A,B,C,D,E,F,G};
           A  B  C  D  E  F  G      (弧头)
        A [0][1][0][1][0][0][0]
        B [0][0][0][1][1][0][0]
        C [0][0][0][1][0][1][0]    
        D [0][0][1][0][0][0][1]
        E [0][0][0][0][0][0][1]
        F [0][0][0][0][0][0][1]
        G [0][0][0][0][0][1][0]
       (弧尾) 
        在二维数组E[i][j]值为1,则表示顶点V[i]到V[j]有边
        注意:由于不存在自己到自己的边,左对角线上的值一定为0
        如果存储的是无向图则二维数组的值沿对角线对称,可以压缩成一维数组(参考矩阵压缩)
        优点:可以方便的计算顶点的入度与出度
        缺点:当图是稀疏图时,会非常的浪费存储空间

    邻接表:(链式+顺序)
        边:
            顶点下标
            指向下一条边的地址
        顶点:
            顶点数据
            指向第一条边的指针
        图:
            由顶点组成的数组
            顶点数量
        优点:节约存储空间,计算出度方便
        缺点:计算入度麻烦 

    十字链表:
        是一种专门存储有向图的一种结构
        边
            弧尾下标
            弧头下标
            指向弧尾相同的下一条边
            指向弧头相同的下一条边
        顶点:
            顶点数据
            指向第一条出度的边
            指向第一条入度的边
        图:
            由顶点组成的数组
            顶点数量
        优点:节约空间、计算出入度很方便

    邻接多重表:
        是一种专门存储无向图的一种结构
        边:
            i j 两个相互依附的顶点的下标
            inext 指向下一条依附于i顶点的边
            jnext 指向下一条依附于j顶点的边
        顶点:
            顶点数据
            指向与顶点有关的一条边的指针
        图:
            由顶点组成的数组
            顶点数量

上三、下三角角矩阵的实现

#include <stdio.h>
#include <stdlib.h>

int main(int argc,const char* argv[])
{
	int n = 0;
	printf("请输入矩阵的阶数:");
	scanf("%d",&n);

	int arr[n][n];
	int ret[n*(n+1)/2];

	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(j > i)
			{
				arr[i][j] = 0;
			}
			else
			{
				arr[i][j] = rand()%9+1;	
				ret[(i+1)*i/2+j] = arr[i][j];
				//arr[i][j] = (i+1)*i/2+j;	
			}
			printf("%d ",arr[i][j]);
		}
		printf("\n");
	}
	printf("---------------\n");
	for(int i=0; i<(n+1)*n/2; i++)
	{
		printf("%d ",ret[i]);	
	}
}

对称、对角矩阵的实现

#include <stdio.h>
#include <stdlib.h>

int main(int argc,const char* argv[])
{
	int n = 0;
	printf(">>>");
	scanf("%d",&n);
	int arr[n][n];

	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n; j++)
		{
			if(abs(i-j) <= 1)
			{
				//arr[i][j] = rand()%9+1;	
				arr[i][j] = i*2+j;
			}
			else
			{
				arr[i][j] = 0;	
			}
			printf("%d ",arr[i][j]);
		}
		printf("\n");
	}
}

三元组的实现

#include <stdio.h>
#include <stdlib.h>

//	三元组元素
typedef struct Node
{
	int row;	//	行
	int col;	//	列
	int val;	//	值
}Node;

//	三元组顺序表
typedef struct Triad
{
	Node* arr;	//	存储所有的三元组元素
	int cnt;	//	元素个数
	int r_max;	//	二维数组行数
	int c_max;	//	二维数组列数
}Triad;

//	压缩成三元组结构
Triad* create_triad(int r,int c,int (*arr)[c],int cnt)
{
	//	申请内存和初始化成员
	Triad* triad = malloc(sizeof(Triad));
	triad->arr = malloc(sizeof(Node)*cnt);
	triad->r_max = r;
	triad->c_max = c;

	int index = 0;
	//	遍历待压缩的二维数组 存储数据到三元组数组中
	for(int i=0; i<r; i++)
	{
		for(int j=0; j<c; j++)
		{
			if(arr[i][j])
			{
				triad->arr[index].row = i;
				triad->arr[index].col = j;
				triad->arr[index++].val = arr[i][j];
			}
		}
	}
	triad->cnt = index;
	return triad;
}

void show_triad(Triad* triad)
{
	for(int i=0; i<triad->cnt; i++)
	{
		printf("%d %d %d\n",triad->arr[i].val,triad->arr[i].row,
			triad->arr[i].col);	
	}
}

邻接矩阵的实现

        实现需要队列  .h .c

#ifndef LIST_QUEUE_H
#define LIST_QUEUE_H

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define TYPE int

typedef struct Node
{
	TYPE data;
	struct Node* next;
}Node;

typedef struct ListQueue
{
	Node* front;	//	队头指针
	Node* tail;		//	队尾指针
	size_t cnt;		//	结点数量
}ListQueue;

Node* create_node(TYPE data);
//	创建
ListQueue* create_list_queue(void);
//	队空
bool empty_list_queue(ListQueue* queue);
//	入队
void push_list_queue(ListQueue* queue,TYPE val);
//	出队
bool pop_list_queue(ListQueue* queue);
//	队头
TYPE front_list_queue(ListQueue* queue);
//	队尾
TYPE back_list_queue(ListQueue* queue);
//	数量
size_t size_list_queue(ListQueue* queue);
//	销毁
void destory_list_queue(ListQueue* queue);

#endif//LIST_QUEUE_H
#include "list_queue.h"

Node* create_node(TYPE data)
{
	Node* node = malloc(sizeof(Node));
	node->data = data;
	node->next = NULL;
	return node;
}

//	创建
ListQueue* create_list_queue(void)
{
	ListQueue* queue = malloc(sizeof(ListQueue));
	queue->front = NULL;
	queue->tail = NULL;
	queue->cnt = 0;
	return queue;
}

//	队空
bool empty_list_queue(ListQueue* queue)
{
	return 0 == queue->cnt;	
}

//	入队
void push_list_queue(ListQueue* queue,TYPE val)
{
	Node* node = create_node(val);
	if(empty_list_queue(queue))
	{
		queue->front = node;
		queue->tail = node;
	}
	else
	{
		queue->tail->next = node;
		queue->tail = node;
	}
	queue->cnt++;
}

//	出队
bool pop_list_queue(ListQueue* queue)
{
	if(empty_list_queue(queue)) return false;
	Node* temp = queue->front;
	queue->front = temp->next;
	queue->cnt--;
	if(0 == queue->cnt)	queue->tail = NULL;
	free(temp);
	return true;
}

//	队头
TYPE front_list_queue(ListQueue* queue)
{
	return queue->front->data;	
}

//	队尾
TYPE back_list_queue(ListQueue* queue)
{
	return queue->tail->data;	
}

//	数量
size_t size_list_queue(ListQueue* queue)
{
	return queue->cnt;
}
//	销毁
void destory_list_queue(ListQueue* queue)
{
	while(pop_list_queue(queue));
	free(queue);
}

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list_queue.h"

//	邻接矩阵
typedef struct Graph
{
	char* v;	//	顶点一维数组
	char* e;	//	边的二维数组
	int cnt;	//	顶点数量
}Graph;

Graph* create_graph(const char* str)
{
	//	申请邻接矩阵内存
	Graph* graph = malloc(sizeof(Graph));
	//	计算顶点数量
	graph->cnt = strlen(str);
	//	申请顶点所需内存
	graph->v = malloc(graph->cnt+1);
	//	存储顶点
	strcpy(graph->v,str);
	//	申请存储边所需内存
	graph->e = calloc(graph->cnt,graph->cnt);
	return graph;
}

//	添加边
bool add_edge(Graph* graph,char v1,char v2)
{
	int x = -1, y = -1;
	for(int i=0; i<graph->cnt; i++)
	{
		if(graph->v[i] == v1)	x = i;
		if(graph->v[i] == v2)  	y = i;
	}
	if(-1==x || -1==y) return false;

	//	有向图
	graph->e[x*graph->cnt+y] = 1;
	//	无向图只需要再加一句
//	graph->e[y*graph->cnt+x] = 1;
	return true;
}

void show_graph(Graph* graph)
{
	printf(" ");
	for(int i=0; i<graph->cnt; i++)
		printf(" %c",graph->v[i]);
	printf("\n");
	for(int i=0; i<graph->cnt; i++)
	{
		printf("%c ",graph->v[i]);
		for(int j=0; j<graph->cnt; j++)
		{
			printf("%hhd ",graph->e[i*graph->cnt+j]);	
		}
		printf("\n");
	}
}

//	计算顶点的出度
int od_graph(Graph* graph,char v)
{
	for(int x=0; x<graph->cnt; x++)
	{
		if(v == graph->v[x])	
		{
			int od = 0;
			for(int y=0; y<graph->cnt; y++)
			{
				od += graph->e[x*graph->cnt+y];
			}
			return od;
		}
	}
	return -1;
}
//	计算顶点的入度
int id_graph(Graph* graph,char v)
{
	for(int y=0; y<graph->cnt; y++)
	{
		if(v == graph->v[y])
		{
			int id = 0;
			for(int x=0; x<graph->cnt; x++)
			{
				id += graph->e[x*graph->cnt+y];
			}
			return id;
		}
	}
	return -1;
}

//	从第i个顶点开始进行深度优先遍历
void _DFS(Graph* graph,int i,char* vflag)
{
	if(vflag[i]) return;

	printf("%c ",graph->v[i]);
	vflag[i] = 1;

	for(int j=0; j<graph->cnt; j++)
	{
		if(graph->e[i*graph->cnt+j])
		{
			_DFS(graph,j,vflag);	
		}
	}
}
//	深度优先遍历 
void DFS_show(Graph* graph)
{
	//顶点的标志位
	char vflag[graph->cnt];
	memset(vflag,0,graph->cnt);

	for(int i=0; i<graph->cnt; i++)
		_DFS(graph,i,vflag);
}

//	广度优先遍历
void BFS_show(Graph* graph)
{
	//顶点的标志位
	char vflag[graph->cnt];
	memset(vflag,0,graph->cnt);
	
	ListQueue* queue = create_list_queue();
	for(int i=0; i<graph->cnt; i++)
	{
		if(!vflag[i])
		{
			push_list_queue(queue,i);
			vflag[i] = 1;
		}
		while(!empty_list_queue(queue))
		{
			int j = front_list_queue(queue);
			printf("%c ",graph->v[j]);
			pop_list_queue(queue);

			for(int y=0; y<graph->cnt; y++)
			{
				if(graph->e[j*graph->cnt+y] && !vflag[y])
				{
					push_list_queue(queue,y);
					vflag[y] = 1;
				}
			}
		}
	}
}

int main(int argc,const char* argv[])
{
	Graph* graph = create_graph("ABCDEFGX");	
	add_edge(graph,'A','B');
	add_edge(graph,'A','D');
	add_edge(graph,'B','D');
	add_edge(graph,'B','E');
	add_edge(graph,'C','D');
	add_edge(graph,'C','F');
	add_edge(graph,'D','C');
	add_edge(graph,'D','G');
	add_edge(graph,'E','G');
	add_edge(graph,'F','G');
	add_edge(graph,'G','F');
	add_edge(graph,'X','E');
	show_graph(graph);

	DFS_show(graph);
	printf("\n");
	BFS_show(graph);
}

图的实现

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "list_queue.h"

//	边
typedef struct Edge
{
	int index;			//	边指向顶点的下标
	struct Edge* next;	//	指向下一条边
}Edge;

//	创建到index顶点的边
Edge* create_edge(int index)
{
	Edge* edge = malloc(sizeof(Edge));
	edge->index = index;
	edge->next = NULL;
	return edge;
}

//	顶点
typedef struct Vertex
{
	char vertex;	//	顶点数据
	Edge* first;	//	指向该顶点的第一条边
}Vertex;

//	图
typedef struct Graph
{
	Vertex* v;		//	顶点数组
	int cnt;		//	顶点数量
}Graph;

//	创建图
Graph* create_graph(const char* str)
{
	//	申请图所需内存
	Graph* graph = malloc(sizeof(Graph));
	//	计算顶点数量
	graph->cnt = strlen(str);
	//	申请存储顶点数组所需内存
	graph->v = malloc(sizeof(Vertex)*graph->cnt);
	//	初始化顶点
	for(int i=0; i<graph->cnt; i++)
	{
		graph->v[i].vertex = str[i];
		graph->v[i].first = NULL;
	}
	return graph;
}

//	添加边
bool add_edge_graph(Graph* graph,char v1,char v2)
{
	for(int i=0; i<graph->cnt; i++)
	{
		if(v1 == graph->v[i].vertex)	//	v1下标 i	
		{
			//	对第i个顶点的first进行头添加
			for(int j=0; j<graph->cnt; j++)
			{
				if(v2 == graph->v[j].vertex)	//	v2下标 j
				{
					//	创建指向j的边
					Edge* edge = create_edge(j);
					edge->next = graph->v[i].first;
					graph->v[i].first = edge;
					return true;
				}
			}
		}
	}
	return false;
}

//	计算出度
int od_graph(Graph* graph,char v)
{
	int od = 0;
	for(int i=0; i<graph->cnt; i++)
	{
		if(v == graph->v[i].vertex)
		{
			for(Edge* e=graph->v[i].first; e; e=e->next)
			{
				od++;	
			}
			return od;
		}
	}
	return -1;
}

//	入度
int id_graph(Graph* graph,char v)
{
	//	寻找v的下标
	for(int i=0; i<graph->cnt; i++)
	{
		if(v == graph->v[i].vertex)
		{
			int id = 0;
			for(int j=0; j<graph->cnt; j++)
			{
				for(Edge* e=graph->v[j].first; e; e=e->next)
				{
					if(e->index == i) id++;	
				}
			}
			return id;
		}
	}
	return -1;
}

void _DFS(Graph* graph,int i,char* vflag)
{
	if(vflag[i]) return;
	printf("%c ",graph->v[i].vertex);
	vflag[i] = 1;

	for(Edge* e=graph->v[i].first; e; e=e->next)
	{
		_DFS(graph,e->index,vflag);	
	}
}

//	深度优先
void DFS_show(Graph* graph)
{
	char vflag[graph->cnt];
	memset(vflag,0,graph->cnt);

	for(int i=0; i<graph->cnt; i++)
	{
		_DFS(graph,i,vflag);		
	}
	printf("\n");
}

//	广度优先
void BFS_show(Graph* graph)
{
	ListQueue* queue = create_list_queue();
	char vflag[graph->cnt];
	memset(vflag,0,graph->cnt);

	for(int i=0; i<graph->cnt; i++)
	{
		if(!vflag[i])
		{
			push_list_queue(queue,i);
			vflag[i] = 1;
		}
		while(!empty_list_queue(queue))
		{
			int j = front_list_queue(queue);
			printf("%c ",graph->v[j].vertex);
			pop_list_queue(queue);
			
			for(Edge* e=graph->v[j].first; e; e=e->next)
			{
				if(!vflag[e->index])
				{
					push_list_queue(queue,e->index);
					vflag[e->index] = 1;
				}
			}
		}
	}
	destory_list_queue(queue);
	printf("\n");
}

void show_graph(Graph* graph)
{
	for(int i=0; i<graph->cnt; i++)
	{
		printf("index:%d v:%c e:",i,graph->v[i].vertex);
		for(Edge* e=graph->v[i].first; e; e=e->next)
		{
			printf("%c ",graph->v[e->index].vertex);	
		}
		printf(" od:%d id:%d",od_graph(graph,graph->v[i].vertex),
			id_graph(graph,graph->v[i].vertex));
		printf("\n");
	}
}
int main(int argc,const char* argv[])
{
	Graph* graph = create_graph("ABCDEFGH");
	add_edge_graph(graph,'A','D');
	add_edge_graph(graph,'A','B');
	add_edge_graph(graph,'B','D');
	add_edge_graph(graph,'B','C');
	add_edge_graph(graph,'C','D');
	add_edge_graph(graph,'D','E');
	add_edge_graph(graph,'D','F');
	add_edge_graph(graph,'E','G');
	add_edge_graph(graph,'G','H');
	add_edge_graph(graph,'F','H');
	show_graph(graph);
	DFS_show(graph);
	BFS_show(graph);

}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值