Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
东方怂天
DataStructure
提交
a694eae3
D
DataStructure
项目概览
东方怂天
/
DataStructure
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
DataStructure
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
a694eae3
编写于
12月 11, 2019
作者:
东方怂天
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Just do DFS & BFS in a vertex type.
上级
2312a804
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
84 addition
and
7 deletion
+84
-7
Graph/Martix/AdjacencyMatrix.h
Graph/Martix/AdjacencyMatrix.h
+82
-6
Graph/Martix/main.cpp
Graph/Martix/main.cpp
+2
-1
未找到文件。
Graph/Martix/AdjacencyMatrix.h
浏览文件 @
a694eae3
#pragma once
#include <iostream>
#include <stack>
#include <queue>
using
namespace
std
;
#define MAXVERTEX 100 //图的最大顶点数
...
...
@@ -8,6 +10,12 @@ using namespace std;
typedef
char
vertextype
;
//定义定点的存储信息为字符型
typedef
int
arctype
;
//定义边的权值为int型
enum
GraphType
{
Graph
,
Diagraph
};
//图的邻接矩阵的存储结构
typedef
struct
{
...
...
@@ -15,6 +23,7 @@ typedef struct
arctype
Arc
[
MAXVERTEX
][
MAXVERTEX
];
//邻接矩阵
int
Vertexnum
;
//图的当前顶点数
int
Arcnum
;
//图的当前边数
GraphType
GT
;
//图类别
}
MGraph
;
//创建无向网
...
...
@@ -36,6 +45,11 @@ void CreateMGraph(MGraph &G)
exit
(
0
);
}
cout
<<
"输入图类别(0-无向图,1有向图):"
<<
endl
;
bool
IsDiagraph
;
cin
>>
IsDiagraph
;
G
.
GT
=
IsDiagraph
?
Diagraph
:
Graph
;
G
.
Arcnum
=
0
;
//输入顶点信息
...
...
@@ -55,7 +69,7 @@ void CreateMGraph(MGraph &G)
}
cout
<<
"输入边信息:"
<<
endl
;
cout
<<
"
起始点 结束点 权值
"
<<
endl
;
cout
<<
"
@ 起始点 结束点 权值 (#结束)
"
<<
endl
;
//连接边
char
flag
;
int
i
,
j
,
w
;
...
...
@@ -68,7 +82,12 @@ void CreateMGraph(MGraph &G)
}
cin
>>
i
>>
j
>>
w
;
G
.
Arc
[
i
][
j
]
=
w
;
//赋权
G
.
Arcnum
++
;
//边数增加
//无向图
if
(
G
.
GT
==
Graph
)
{
G
.
Arc
[
j
][
i
]
=
w
;
//赋权
}
G
.
Arcnum
++
;
//边数增加
}
}
...
...
@@ -92,7 +111,6 @@ void PrintfMGraph(MGraph G)
}
else
{
cout
<<
G
.
Arc
[
i
][
j
]
<<
'\t'
;
}
}
...
...
@@ -114,16 +132,74 @@ void DFS_Rec(MGraph G, int i, bool *Visited)
}
}
//深度遍历最小生成树
void
DFSTraveser
(
MGraph
G
)
//深度搜索(非递归)
void
DFS
(
MGraph
G
,
int
v
,
bool
*
Visited
)
{
stack
<
int
>
Routes
;
Routes
.
push
(
v
);
Visited
[
v
]
=
true
;
cout
<<
G
.
Vertex
[
v
];
while
(
!
Routes
.
empty
())
{
int
i
=
Routes
.
top
(),
j
;
for
(
j
=
0
;
j
<
G
.
Vertexnum
;
j
++
)
{
if
(
G
.
Arc
[
i
][
j
]
!=
INFINITY
&&
!
Visited
[
j
])
{
Routes
.
push
(
j
);
Visited
[
j
]
=
true
;
cout
<<
G
.
Vertex
[
j
];
break
;
}
}
if
(
j
==
G
.
Vertexnum
)
{
Routes
.
pop
();
}
}
}
//深度遍历最小生成树(0-非遍历,1-遍历)
void
DFSTraveser
(
MGraph
G
,
int
Type
)
{
bool
*
Visited
=
new
bool
[
G
.
Vertexnum
];
for
(
int
i
=
0
;
i
<
G
.
Vertexnum
;
i
++
)
{
if
(
!
Visited
[
i
])
{
DFS_Rec
(
G
,
i
,
Visited
);
Type
?
DFS
(
G
,
i
,
Visited
)
:
DFS_Rec
(
G
,
i
,
Visited
);
cout
<<
endl
;
}
}
}
//广度搜索
void
BFSTraveser
(
MGraph
G
)
{
queue
<
int
>
Routes
;
bool
*
Visited
=
new
bool
[
G
.
Vertexnum
];
for
(
int
v
=
0
;
v
<
G
.
Vertexnum
;
v
++
)
{
if
(
!
Visited
[
v
])
{
cout
<<
endl
;
Routes
.
push
(
v
);
cout
<<
G
.
Vertex
[
v
];
Visited
[
v
]
=
true
;
}
while
(
!
Routes
.
empty
())
{
int
i
=
Routes
.
front
(),
j
;
for
(
j
=
0
;
j
<
G
.
Vertexnum
;
j
++
)
{
if
(
G
.
Arc
[
i
][
j
]
!=
INFINITY
&&
!
Visited
[
j
])
{
Routes
.
push
(
j
);
Visited
[
j
]
=
true
;
cout
<<
G
.
Vertex
[
j
];
}
}
Routes
.
pop
();
}
}
}
\ No newline at end of file
Graph/Martix/main.cpp
浏览文件 @
a694eae3
...
...
@@ -19,6 +19,7 @@ int main()
CreateMGraph
(
G
);
//初始化
PrintfMGraph
(
G
);
//打印
DFSTraveser
(
G
);
DFSTraveser
(
G
,
0
);
//DFS搜索
BFSTraveser
(
G
);
return
0
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录