有向图的邻接矩阵表示法(创建,DFS,BFS)
package shiyan; import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner; public class GraphMartix { class Vertext<AnyType>{
char data;
boolean visit;
public Vertext(char d){
this.data=d;
this.visit=false;
}
} Vertext ver[];
int arcs[][];
int verNumble;
//创建图
Scanner sc=new Scanner(System.in); public void creatGraph(){
System.out.println("请输入顶点个数");
verNumble=sc.nextInt();
ver=new Vertext[verNumble];
arcs=new int[verNumble][verNumble];
for(int i=0;i<verNumble;i++){
for(int j=0;j<verNumble;j++){
arcs[i][j]=0;
}
}
System.out.println("请输入顶点数据");
String point=sc.next();
char p[]=point.toCharArray();
for(int i=0;i<p.length;i++){
ver[i]=new Vertext(p[i]);
}
for(int i=0;i<verNumble;i++){
System.out.println("请输入 所有的以"+ver[i].data+"为弧尾的边上的另一端点顶点");
String apoint=sc.next();
char ap[]=apoint.toCharArray();
int ap_id[]=new int[ap.length]; //存放邻接点下标
if(ap[0]=='*')
ap_id[0]=-1;
else{
for(int x=0;x<ap.length;x++){
int y=0;
while(ver[y].data!=ap[x]&&y<ver.length){
y++;
}
ap_id[x]=y;
}
}
if(ap_id[0]==-1)
continue;
for(int j=0;j<ap_id.length;j++){
arcs[i][ap_id[j]]=1;
}
}
} public void DFS(int v){
ver[v].visit=true;
System.out.println(ver[v].data);
for(int i=0;i<ver.length;i++){
if(arcs[v][i]==1&&ver[i].visit==false)
DFS(i);
}
}
public void BFS(int v){
Queue<Integer> q=new LinkedList<Integer>();
q.add(v);
while(!q.isEmpty()){
v=q.poll();
if(ver[v].visit==false){
ver[v].visit=true;
System.out.println(ver[v].data);
}
for(int i=0;i<ver.length;i++){
if(arcs[v][i]==1&&ver[i].visit==false){
q.add(i);
}
}
}
}
public void du(){
int chu=0;
int ru=0;
for(int i=0;i<ver.length;i++){
for(int j=0;j<ver.length;j++){
if(arcs[i][j]==1)
chu++;
if(arcs[j][i]==1)
ru++;
} System.out.println(ver[i].data+"的度为: "+(chu+ru));
chu=0;
ru=0;
}
} public static void main(String[] args) {
GraphMartix gm=new GraphMartix();
gm.creatGraph();
gm.du();
// gm.DFS(0);
gm.BFS(0); } }
有向图的邻接矩阵表示法(创建,DFS,BFS)的更多相关文章
- PTA 7-1 邻接矩阵表示法创建无向图 (20分)
PTA 7-1 邻接矩阵表示法创建无向图 (20分) 采用邻接矩阵表示法创建无向图G ,依次输出各顶点的度. 输入格式: 输入第一行中给出2个整数i(0<i≤10),j(j≥0),分别为图G的顶 ...
- DFS&&BFS
DFS DFS搜索是按照深度的方向搜索,它类似于树的先根遍历,是树的先根遍历的推广. 1.从图的某个顶点v0出发,首先访问v0, 2.找出刚访问过的顶点的第一个未被访问过的邻接点,然后访问该结点,以该 ...
- DFS & BFS
DFS 深度优先 BFS 广度优先 DFS或者BFS都是在联通区域内遍历节点的方法 用在二叉树上DFS有preOreder,inOrder,postOrder,BFS就是层次遍历. 在二叉树上的节点, ...
- 图的建立——邻接矩阵表示(C语言+VC6.0平台)
图的邻接矩阵表示及其建立(无向图) #include <stdio.h> #include <stdlib.h> typedef char VertexType; ...
- DFS/BFS+思维 HDOJ 5325 Crazy Bobo
题目传送门 /* 题意:给一个树,节点上有权值,问最多能找出多少个点满足在树上是连通的并且按照权值排序后相邻的点 在树上的路径权值都小于这两个点 DFS/BFS+思维:按照权值的大小,从小的到大的连有 ...
- 【DFS/BFS】NYOJ-58-最少步数(迷宫最短路径问题)
[题目链接:NYOJ-58] 经典的搜索问题,想必这题用广搜的会比较多,所以我首先使的也是广搜,但其实深搜同样也是可以的. 不考虑剪枝的话,两种方法实践消耗相同,但是深搜相比广搜内存低一点. 我想,因 ...
- ID(dfs+bfs)-hdu-4127-Flood-it!
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4127 题目意思: 给n*n的方格,每个格子有一种颜色(0~5),每次可以选择一种颜色,使得和左上角相 ...
- 基于visual Studio2013解决算法导论之054图的邻接矩阵表示
题目 图的邻接矩阵表示 解决代码及点评 // 图的邻接矩阵表示.cpp : 定义控制台应用程序的入口点. // #include <iostream> #include <l ...
- c语言——单链表分拆——头插法创建链表,尾插法生成链表
#if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; ...
随机推荐
- A Game of Thrones(12) - Eddard
The summons(['sʌm(ə)nz]召唤:传票) came in the hour before the dawn, when the world was still and grey. A ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- 怎样用Google APIs和Google的应用系统进行集成(1)----Google APIs简介
Google的应用系统提供了非常多的应用,比方 Google广告.Google 任务,Google 日历.Google blogger,Google Plus,Google 地图等等非常的多的应用,请 ...
- leetcode第一刷_Permutations II
当有反复元素的时候呢? 不用拍脑袋都会想到一种方法,也是全部有反复元素时的通用处理方法,维护一个set,假设这个元素没增加过就增加,增加过了的忽略掉.可是,在这道题上这个通用方法竟然超时了! 怎么办? ...
- 移动开发平台-应用之星app制作教程
目前在AppStore.GooglePlay等应用商店里已经有以百万计的Apps,应用程序使移动互联网空间得以无限拓展.很多人梦想着AngryBirds式的奇迹在自己身上发生,他们渴望自己开发的应用程 ...
- H3C TE老版本OSPF正确配置
R1配置: ---------------------------------------------------- # sysname RT1# super password level 3 cip ...
- 一次失败的刷题经历:[LeetCode]292之尼姆游戏(Nim Game)(转)
最近闲来无事刷LeetCode,发现这道题的Accept Rate还是挺高的,尝试着做了一下,结果悲剧了,把过程写下来,希望能长点记性.该题的描述翻译成中文如下: 你正在和你的朋友玩尼姆游戏(Nim ...
- Android FragmentStatePageAdapter的使用Demo
上一篇写过FragmentPagerAdapter,这篇来介绍FragmentStatePagerAdapter,那么两者之间有何差别呢: FragmentPagerAdapter很多其它的用于少量界 ...
- js正则匹配html内容
1.得到网页上的链接地址: string matchString = @"<a[^>]+href=\s*(?:'(?<href>[^']+)'|"" ...
- pygame写的弹力球
这是pygame写的弹力球 运行效果: ======================================================== 代码部分: ================= ...