Algorithms 第四版 习题 4.1.16- 4.1.18

原书部分内容见:http://algs4.cs.princeton.edu/home/

4.1.16 计算点的离心率,图的直径,半径,中心

4.1.18 计算图的围长

定义:

点的离心率:图中任意一点v,v的离心率是图中其他点到v的所有最短路径中最大值。

图的直径:图中所有点的离心率的最大值。

图的半径:图中所有点的离心率的最小值。

图的中心:图中离心率长度等于半径的点。

图的围长:如果图中有环,围长则为所有环的长度的最小值。


一般解法:

1. 分别以图中每个点为根进行 广度优先搜索BFS,每次BFS计算并用数组 e[] 保存好每个点的离心率。之后迭代数组即可得知直径,半径,中心。

2. 对于围长,分别以图中每个点为根进行BFS,每次BFS计算并用数组 g [] 保存包含该点的最小环的长度。之后迭代数组即可得知图的围长。

以上两种操作可以在同一迭代中进行。

另外,为方便计算,标记 点 是否访问过时 改用具体的数字- 点到根的距离,即点到根的最短路径 - 代替使用布尔值标记。即定义 int [] marked = new marked [G.V]

为了计算包含某个点的最小环,对于每个层次的BFS,需要知道该层次中各点,例如u,的前驱是哪个点:如果有边连接v 到 u,如果u不是v的前驱,则说明出现了环,

该环的长度为 marked[v] + marked[u] +1。


因为要对每个点进行BFS,所以时间复杂度为 O(V *(V+E)),V为点数,E为边数。


代码如下:

package com.cupid.algorithm.graph.algorithms4th;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class GraphProperties {
	
	// Rather than using boolean values to indicate whether a vertex was visited,
	// use a Integer array to mark each visited vertex.
	// If marked[v] = -1 , vertex v has not been visited.
	// If v is root of a BFS, marked[v] = 0;
	// Other positive values represent for the shortest path length from source(root) to a particular vertex.
	private int[] marked;
	// Integer array e is used to store eccentricity  of every vertex.
	private int[] e;
	// Integer array g is used to store minimum length of circle(s) involving the root vertex.
	private int[] g;
	// For calculating minimum length of circle(s), Integer array p is used to store predecessor of every vertex. 
	private int[] p;
	private Graph myGraph;
	private int center; 
	
	public GraphProperties(Graph G){
		e = new int[G.V()];
		g = new int[G.V()];
		p = new int[G.V()];
		marked = new int[G.V()];
		this.myGraph = G;
		
		// Calculate the eccentricity and girth for the tree rooted at each vertex
		// to decide diameter, radius, center, and girth of a given graph.
		// It takes O(V*(V+E))
		for(int i=0;i<myGraph.V();i++){
			breathFirstSearch(i,e,g);
		}
	}
	
	public int eccentricity(int v){
		return e[v];
	}
	
	public int girth(){
		int min = Integer.MAX_VALUE;
		for(int i=0;i<g.length;i++){
			if(g[i] < min){
				min = g[i];
			}
		}
		return min;
	}
	
	// The diameter of a graph is the maximum eccentricity
	// of any vertex.
	public int diameter(){
		int max = Integer.MIN_VALUE;
		for(int i=0;i<e.length;i++){
			if(e[i] > max){
				max = e[i];
			}
		}
		return max;
	}
	
	// The radius of a graph is the smallest eccentricity of any vertex. A center is
	// a vertex whose eccentricity is the radius.
	public int radius(){
		int min = Integer.MAX_VALUE;
		for(int i=0;i<e.length;i++){
			if(e[i] < min){
				min = e[i];
				center = i;
			}
		}
		return min;
	}
	
	// Calculate the eccentricity and girth for a given vertex.
	// Definition from Algorithms 4th edition:
	
	// The eccentricity of a vertex v is the the length of the shortest path from that vertex
	// to the farthest vertex from v 
	
	// The girth of a graph is the length of its shortest cycle. If a graph is acyclic, then its
	// girth is infinite.
	private void breathFirstSearch(int v,int[] e,int[] g){
		
		// Initialize int[] marked before a BFS is going to commence.
		for(int i=0;i<myGraph.V();i++){
			marked[i] = -1;
		}
		// A standard BFS begins.
		Queue<Integer> q = new LinkedList<Integer>();
		int eccen = 0;
		int girth = Integer.MAX_VALUE;
		marked[v] = 0;
		q.add(v);
		while(!q.isEmpty()){
			int u = q.poll();
			for(Integer w: myGraph.adj(u)){
				if(marked[w] <0){
					// Each level of BFS should increase shortest path length by 1
					marked[w] = marked[u] +1;
					// Assign a shortest path length to an eccentricity.
					eccen = marked[w];
					// w's parent u was found.
					p[w] = u;
					q.add(w);
				}else{
					// If the marked vertex w is not u's parent,
					// there is a cycle here.
					if(w != p[u]){
						// Decide that whether this cycle's length is greater than
						// the smallest one that has been discovered.
						// If no, this cycle is the smallest one.
						if(girth > marked[w] + marked[u] + 1){
							girth = marked[w] + marked[u] + 1;
						}
					}
				}
			}
		}
		// Store eccentricity and girth for root vertex in every BFS.
		e[v] = eccen;
		g[v] = girth;
	}
	
	
	public int getCenter() {
		return center;
	}

	public static void main(String[] args) {
		File file = new File("d:\\tinyG6.txt");
		try {
			Scanner in = new Scanner(file);
			Graph G = new Graph(in);
			System.out.println(G);
			GraphProperties gps = new GraphProperties(G);
			System.out.println("The diameter is: " + gps.diameter());
			System.out.println("The radius is: " +gps.radius());
			System.out.println("The center is vertex No." + gps.getCenter());
			if(gps.girth() == Integer.MAX_VALUE){
				System.out.println("The girth is infinite since the graph is acyclic.");
			}else{
				System.out.println("The girth is: " + gps.girth());
			}
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}
	}

}

测试数据,以邻接链表表示:

0: 1 
1: 3 2 0 
2: 4 3 1 
3: 5 4 2 1 
4: 2 3 
5: 7 6 3 
6: 8 5 
7: 8 5 
8: 7 6 

PS:关于具体的图的实现(如每个点的邻接链表)可以参考上面的原书网址。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
个人觉得是我见过的最简单易懂的算法入门书籍。 以前搜刮过几本算法竞赛书,但是难度终归太大【好吧,其实是自己太懒了】。 略翻过教材,大多数水校的教材,大家懂的。好一点的也是那本国内的经典,不是说它写的不好,只是没有这一本好。 本书Java实现,配有大量的图解,没有一句难懂的话,而且全都是模块化实现。 讲的都是实用算法,没有那些高大上听着名字就让人感到很害怕的东西,个人觉得比CLRS实用性要强,更加适合入门的学习。 大一,推荐这本书入门 【有C语言基础即可,自己去搜索下如何用Java写出Hello World就没有问题】 大二,推荐这本书从头到尾好好读一遍,做下上千道的课后习题 【后面的有点小难度,但是难度不大值得一做,听起来很多的样子,用心去做,相信很快就可以做完的】。 大三,推荐这本书,重新温习已知算法,为找工作,考研做准备。 【可以试着自己在纸上全部实现一遍】 大四,依旧推荐这本书,没事重温经典,当手册来查也不错。 Sedgwick 红黑树的发现者,Donald E.Knuth 的得意门生,对各种算法都有比较深入的研究,他的书,我想不会太差。 也许对于数据结构的学习涉及的内容比较少,没有动态规划,图论也只是讲了很基础的东西,字符串中KMP弄的过于复杂(对比于acm)。但是瑕不掩瑜,对于绝大部分内容真的讲的超级清楚,完美的图解,就像单步调试一样,也许是一本不需要智商就能看懂的算法书(习题应该略有难度,还没有做,打算上Princeton的公开课时同步跟进)。至少这是一本让我这个算法渣渣看了爱不释手,怦然心动的书。 完美学习资源: 官方主页:http://algs4.cs.princeton.edu/home/ Coursera公开课:https://www.coursera.org/course/algs4partI (听说已经开课两期了,最近即将开课的时间是2014/09/05号那期,希望有兴趣的同学一起来学习)。 MOOC平台(笔记、讨论等): http://mooc.guokr.com/course/404/Algorithms--Part-I/ http://mooc.guokr.com/course/403/Algorithms--Part-II/ 不得不吐槽,他的lecture比他的书好,他本人讲的课更是一绝。 互补课程: 斯福坦的Algorithms: Design and Analysis, http://mooc.guokr.com/course/157/Algorithms--Design-and-Analysis--Part-1/ 快毕业了才接触到豆瓣和MOOC,看到很多经典的书籍都是推荐大学一二年级的学生看,每每想到自己却连书皮都没有摸过,就深感惭愧。 我们都老的太快,却聪明得太迟。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值