转载一个图数据结构题目

用1、2、2、3、4、5这六个数字,用java写一个main函数,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"与"5"不能相连.

 

采用二维数组定义图结构,最后的代码是:

import java.util.Iterator;
import java.util.TreeSet;

public class TestQuestion {

private String[] b = new String[]{"1", "2", "2", "3", "4", "5"};
private int n = b.length;
private boolean[] visited = new boolean[n];
private int[][] a = new int[n][n];
private String result = "";
private TreeSet set = new TreeSet();

public static void main(String[] args) {
new TestQuestion().start();
}

private void start() {

// Initial the map a[][]
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i == j) {
a[i][j] = 0;
} else {
a[i][j] = 1;
}
}
}

// 3 and 5 can not be the neighbor.
a[3][5] = 0;
a[5][3] = 0;

// Begin to depth search.
for (int i = 0; i < n; i++) {
this.depthFirstSearch(i);
}

// Print result treeset.
Iterator it = set.iterator();
while (it.hasNext()) {
String string = (String) it.next();
// "4" can not be the third position.
if (string.indexOf("4") != 2) {
System.out.println(string);
}
}
}

private void depthFirstSearch(int startIndex) {
visited[startIndex] = true;
result = result + b[startIndex];
if (result.length() == n) {
// Filt the duplicate value.
set.add(result);
}
for(int j = 0; j < n; j++) {
if (a[startIndex][j] == 1 && visited[j] == false) {
depthFirstSearch(j);
} else {
continue;
}
}

// restore the result value and visited value after listing a node.
result = result.substring(0, result.length() -1);
visited[startIndex] = false;
}
}

 

 

转自 javaeye:http://www.javaeye.com/topic/55873

...... ( B )3. 有8个结点的无向最多有 条边。 A.14 B. 28 C. 56 D. 112 ( C )4. 有8个结点的无向连通最少有 条边。 A.5 B. 6 C. 7 D. 8 ( C )5. 有8个结点的有向完全有 条边。 A.14 B. 28 C. 56 D. 112 ( B )6. 用邻接表表示进行广度优先遍历时,通常是采用 来实现算法的。 A.栈 B. 队列 C. 树 D. ...... 二、填空题(每空1分,共20分) 1. 有 邻接矩阵 、 邻接表 等存储结构,遍历有 深度优先遍历 、 广度优先遍历 等方法。 2. 有向G用邻接表矩阵存储,其第i行的所有元素之和等于顶点i的 出度 。 3. 如果n个顶点的一个环,则它有 n 棵生成树。 4. n个顶点e条边的,若采用邻接矩阵存储,则空间复杂度为 O(n2) 。 5. n个顶点e条边的,若采用邻接表存储,则空间复杂度为 O(n+e) 。 ....... 1. 【严题集7.1①】已知如所示的有向,请给出该的: 每个顶点的入/出度; 邻接矩阵; 邻接表; 逆邻接表。 2. 【严题集7.7②】请对下的无向带权: 写出它的邻接矩阵,并按普里姆算法求其最小生成树; 写出它的邻接表,并按克鲁斯卡尔算法求其最小生成树。 ........ 五、算法设计题(每题10分,共30分) 1. 【严题集7.14③】编写算法,由依次输入的顶点数目、弧的数目、各顶点的信息和各条弧的信息建立有向的邻接表。 解:Status Build_AdjList(ALGraph &G) //输入有向的顶点数,边数,顶点信息和边的信息建立邻接表 { InitALGraph(G); scanf("%d",&v); if(v<0) return ERROR; //顶点数不能为负 G.vexnum=v; scanf("%d",&a); if(a<0) return ERROR; //边数不能为负 G.arcnum=a; for(m=0;m<v;m++) G.vertices[m].data=getchar(); //输入各顶点的符号 for(m=1;m<=a;m++) { t=getchar();h=getchar(); //t为弧尾,h为弧头 if((i=LocateVex(G,t))<0) return ERROR; if((j=LocateVex(G,h))nextarc;q=q->nextarc); q->nextarc=p; } p->adjvex=j;p->nextarc=NULL; }//while return OK; }//Build_AdjList 2. 【严题集7.15③】试在邻接矩阵存储结构上实现的基本操作:DeleteArc(G,v,w)。 (刘提示:删除所有从第i个顶点出发的边的方法是 将邻接矩阵的第i行全部置0 ) ........
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值