⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 run3.java

📁 使用Java实现的Graph的迭代加深的深度优先搜索
💻 JAVA
字号:
/**
 * 
 */
package run;

import java.util.List;
import java.util.Scanner;

import digraph.Graph;
import digraph.Node;

/**
 * @author zhangli
 *
 */
public class Run3 {
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		DataParser  reader = new DataParser();
		Graph graph = reader.readGraph("data/wikipedia.idx");
		
		System.out.println("Load Success!! Please enter command:");
		
		Scanner stdin = new Scanner(System.in);
		while (stdin.hasNext())
		{
			String command = stdin.next();
			if (command.equals("printShortestPath"))
			{
				String name1 = stdin.next();
				String name2 = stdin.next();
				
				System.out.printf("%s to %s", name1, name2);
				
				Node node1 = graph.getNode(name1);
				Node node2 = graph.getNode(name2);
				if (node1 == node2)
				{
					System.out.printf("\nsrc is the same to dest!\n\n");
					continue;
				}
				if (node1 == null || node2 == null)
				{
					System.out.printf("\nThere is none path from %s to %s\n\n", name1, name2);
					continue;
				}
				List<List<Node>> nodeLists = graph.getShortestPath(node1, node2);
				if (nodeLists.size() == 0)
				{
					System.out.printf("\nThere is none path from %s to %s\n\n", name1, name2);
					continue;
				}
				
				System.out.printf(" (%d paths there)\n", nodeLists.size());
				printList(nodeLists);
				System.out.println();
			}
			else if (command.equals("exit"))
			{
				break;
			}
		}
	}
	
	public static void printList(List<List<Node>> nodeLists)
	{
		for (List<Node> nodeList : nodeLists)
		{
			System.out.print("[");
			for (int i=0; i<nodeList.size()-1; i++)
			{
				Node node = nodeList.get(i);
				System.out.print(node.getName() + ",");
			}
			Node node = nodeList.get(nodeList.size()-1);
			System.out.println(node.getName() + "]");
			
		}
		
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -