import java.util.*;

public class Main{
static int MAX_VERTEXNUM = 100;
static int [] visited = new int[MAX_VERTEXNUM];
public static void main(String [] args){
Graph G = new Graph();
creatGraph(G);
output(G);
for(int i=0;i<G.vertex_num;i++)
visited[i]=0;
System.out.println("DFS遍历的结果是:");
dfs(G,0);//从G.node_list[0]开始DFS遍历
}
static void creatGraph(Graph G){
Scanner in = new Scanner(System.in);
System.out.println("请输入顶点数v和边数e,(v e):");
G.vertex_num = in.nextInt();
G.edge_num= in.nextInt();
System.out.println("请输入各顶点信息:");
for(int i=0;i<G.vertex_num;i++){
G.node_list[i] = new VertexNode();
G.node_list[i].date = in.next();
G.node_list[i].first_edge = null; //很重要
}
System.out.println("请输入各边信息(以空格隔开):");
for(int i=0;i<G.edge_num;i++){
EdgeNode p = new EdgeNode();
String str1 = in.next();
String str2 = in.next();
int v1 = locateVex(G,str1);
int v2 = locateVex(G,str2);
p.vertex = v1;
p.next = G.node_list[v2].first_edge;
G.node_list[v2].first_edge = p;
EdgeNode q = new EdgeNode();//如果是有向图,则不存在下面的代码(上面的代码的存储顺序可能有变化)
q.vertex = v2;
q.next = G.node_list[v1].first_edge;
G.node_list[v1].first_edge = q;
}
}
static int locateVex(Graph G,String s){
for(int i=0;i<G.vertex_num;i++){
if(G.node_list[i].date.equals(s))
return i;
}
return -1;
}
static void output(Graph G){
System.out.println("输出邻接表存储情况:");
EdgeNode p = new EdgeNode();
for(int i=0;i<G.vertex_num;i++){
System.out.print(G.node_list[i].date);
p = G.node_list[i].first_edge;
while(p!=null){
System.out.print("->"+G.node_list[p.vertex].date);
p = p.next;
}
System.out.println();
}
}
static void dfs(Graph G,int k){
System.out.println(G.node_list[k].date);
visited[k]=1;
EdgeNode p = new EdgeNode();
p = G.node_list[k].first_edge;
while(p!=null){
if(visited[p.vertex]!=1)
dfs(G,p.vertex);
p = p.next;
}
}
}
//顶点存储
class VertexNode{
String date;
EdgeNode first_edge = new EdgeNode(); }
//边存储
class EdgeNode{
int vertex;
EdgeNode next;
}
//图存储
class Graph{
VertexNode [] node_list = new VertexNode[100];
int vertex_num,edge_num;
} //输出结果:注意:(邻接表的保存顺序与输入边的顺序有关)

邻接表存储图,DFS遍历图的java代码实现的更多相关文章

  1. 数据结构(11) -- 邻接表存储图的DFS和BFS

    /////////////////////////////////////////////////////////////// //图的邻接表表示法以及DFS和BFS //////////////// ...

  2. PTA 邻接表存储图的广度优先遍历(20 分)

    6-2 邻接表存储图的广度优先遍历(20 分) 试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(V ...

  3. PTA 邻接表存储图的广度优先遍历

    试实现邻接表存储图的广度优先遍历. 函数接口定义: void BFS ( LGraph Graph, Vertex S, void (*Visit)(Vertex) ) 其中LGraph是邻接表存储的 ...

  4. 数据结构之---C语言实现图的邻接表存储表示

    // 图的数组(邻接矩阵)存储表示 #include <stdio.h> #include <stdlib.h> #include <string.h> #defi ...

  5. 图的邻接表存储表示(C)

    //---------图的邻接表存储表示------- #include<stdio.h> #include<stdlib.h> #define MAX_VERTEXT_NUM ...

  6. 图的邻接表存储 c实现

    图的邻接表存储 c实现 (转载) 用到的数据结构是 一个是顶点表,包括顶点和指向下一个邻接点的指针 一个是边表, 数据结构跟顶点不同,存储的是顶点的序号,和指向下一个的指针 刚开始的时候把顶点表初始化 ...

  7. c++ 由无向图构造邻接表,实现深度优先遍历、广度优先遍历。

    /* 首先,根据用户输入的顶点总数和边数,构造无向图,然后以用户输入的顶点 为起始点,进行深度优先.广度优先搜索遍历,并输出遍历的结果. */ #include <stdlib.h> #i ...

  8. SDUT 2142 数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    数据结构实验之图论二:基于邻接表的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Problem Descript ...

  9. DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储

    题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...

随机推荐

  1. Word Properties <?ref:xdo000X?> - BIP Deskotop 11.119.00.0 (32-bit) with Office 2013 (32-bit) on Win 7 64-bit

    BIP Deskotop 11.119.00.0 (32-bit)Office 2013 (32-bit)Win 7 (64-bit)The current certification matrix ...

  2. string.length()与-1比较为什么会出现匪夷所思的结果

    今天调试程序发现了个匪夷所思的事情,-1与string.length()比较永远是-1大,看下面代码 #include<iostream> #include<string> u ...

  3. 1320. Graph Decomposition

    1320 简单并查集 #include <iostream> #include<cstdio> #include<cstring> #include<algo ...

  4. subsets-ii(需要思考,包括了子数组的求法)

    还是有一定难度的. 基本方法,就是用队列,然后不断累加新的数.这是为了不重复而量身定制的. 如果运行重复,是有更简单清晰的方法,就是每次增加考虑一个数字,然后加到本来每一个结果的后面.如下: publ ...

  5. 高斯消元 分析 && 模板 (转载)

    转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...

  6. 深入学习android之AlarmManager

    对应AlarmManage有一个AlarmManagerServie服务程 序,该服务程序才是正真提供闹铃服务的,它主要维护应用程序注册下来的各类闹铃并适时的设置即将触发的闹铃给闹铃设备(在系统中,l ...

  7. struts.custom.i18n.resources 如何配置多个资源文件?

    struts.custom.i18n.resources = resources1,resources2,resources3   配置properties文件

  8. 纯tarjan poj2186

    tarjan,叙叙旧咯 #include<cstdio>#define maxn 50005int e[maxn],ne[maxn],be[maxn],all;int DFN[maxn], ...

  9. 数据库锁机制(一)——概述

    注:内容为自己的推理认知+网络,如有错误和不合理之处,敬请指出. 在多线程环境中我用使用线程锁处理并发问题,而在数据库系统中,并发问题可以细化到事务级别,而DBMS对此的处理方案就是使用锁. 为了适应 ...

  10. UVa 11427 (期望 DP) Expect the Expected

    设d(i, j)表示前i局每局获胜的比例均不超过p,且前i局共获胜j局的概率. d(i, j) = d(i-1, j) * (1-p) + d(i-1, j-1) * p 则只玩一天就就不再玩的概率Q ...