Dijkstra for MapReduce (1)

<math xmlns="http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>,</mo><mi>y</mi><mo>&#x2208;<!-- ∈ --></mo><mi>X</mi>
</math>

准备研究一下Dijkstra最短路径算法Hadoop上用MapReduce实现的过程。首先温习一下普通Dijkstra算法。

1) 数据结构准备:Graph,包括顶点类Vex,优先队列PQ:

package com.wlu.graph;import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;public class Graph {private Map<Vex, Map<Vex, Integer>> Vlist;public Graph(Map<Vex, Map<Vex, Integer>> vvv) {Vlist = vvv;}public static Graph CreateFromFile(String fileName) throws IOException {BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));Map<Vex, Map<Vex, Integer>> _Vlist = new HashMap<Vex, Map<Vex, Integer>>();Map<String, Vex> name2Vex = new HashMap<String, Vex>();for (String line = br.readLine(); line != null; line = br.readLine()) {String[] vs = line.split(" ");// 3 parts in the lineString vsrc = vs[0];String vdes = vs[1];int wsd = Integer.parseInt(vs[2]);// add to the look up tableVex src = null;Vex dest = null;if (name2Vex.containsKey(vsrc)) {src = name2Vex.get(vsrc);} else {src = new Vex(vsrc);name2Vex.put(vsrc, src);}if (name2Vex.containsKey(vdes)) {dest = name2Vex.get(vdes);} else {dest = new Vex(vdes);name2Vex.put(vdes, dest);}// add the new dest node information// System.err.println(src.getName() + "  " + dest.getName() + "  " +// wsd);if (_Vlist.get(src) == null) {HashMap<Vex, Integer> DestMap = new HashMap<Vex, Integer>();DestMap.put(dest, wsd);_Vlist.put(src, DestMap);} else {_Vlist.get(src).put(dest, wsd);}}br.close();return new Graph(_Vlist);}public String toString() {String s = "";for (Vex src : Vlist.keySet()) {s += src.getName() + ": {";for (Vex des : Vlist.get(src).keySet()) {s += des.getName() + " (" + getw(src, des) + ")} {";}s = s.substring(0, s.length() - 1) + "\n";}return s;}public int getw(Vex u, Vex v) {return Vlist.get(u).get(v);}public int getVexNum() {return Vlist.keySet().size();}public Set<Vex> getVexs() {return Vlist.keySet();}// get AdjacencyList of Vex upublic Map<Vex, Integer> AdjacencyList(Vex u) {return Vlist.get(u);}public static void main(String args[]) throws IOException {Graph g = Graph.CreateFromFile("C:/tmp/test.txt");System.out.print(g.getVexNum() + "\n" + g);}}
package com.wlu.graph;public class Vex {private String name;private int value;public Vex(String n, int v) {name = n;value = v;}public Vex(String n) {name = n;value = 0;}@Overridepublic int hashCode(){return name.hashCode();}@Overridepublic boolean equals(Object v){return name.compareTo(((Vex)v).name) == 0;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getValue() {return value;}public void setValue(int value) {this.value = value;}}

 

package com.wlu.graph;import java.util.ArrayList;
import java.util.List;public class PQ {List<Vex> vlist = new ArrayList<Vex>();public void add(Vex v) {// remove the Vex if has exitfor(int i = 0; i < vlist.size(); i++){if(vlist.get(i).getName().compareTo(v.getName()) == 0){vlist.remove(i);break;}}int i = 0;for (; i < vlist.size(); i++) {if (v.getValue() < vlist.get(i).getValue()) {break;}}// insert v to ith positionList<Vex> vlist2 = new ArrayList<Vex>();int j = 0;for (; j < i; j++) {vlist2.add(vlist.get(j));}vlist2.add(v);for (int k = i; k < vlist.size(); k++) {vlist2.add(vlist.get(k));}vlist = vlist2;}public Vex pop() {if (vlist.size() == 0) {return null;}Vex v = vlist.get(0);vlist.remove(0);return v;}public boolean empty() {return vlist.size() == 0;}public void printPQ() {for (Vex v : vlist) {System.out.print(v.getName() + " (" + v.getValue() + ") ");}System.out.println();}public static void main(String args[]) {PQ pq = new PQ();pq.add(new Vex("a", 12));pq.add(new Vex("b", 2));pq.add(new Vex("c", 1));pq.add(new Vex("d", 123));pq.add(new Vex("e", 33));pq.add(new Vex("f", 4));pq.add(new Vex("g", 0));pq.add(new Vex("h", 21));pq.add(new Vex("i", 5));pq.add(new Vex("j", 3));pq.printPQ();System.out.println(pq.pop().getName());pq.printPQ();}}

 2) Dijkstra算法

package com.wlu.dijkstra;import java.io.IOException;
import java.util.HashMap;
import java.util.Map;import com.wlu.graph.Graph;
import com.wlu.graph.PQ;
import com.wlu.graph.Vex;public class DijkstraSeq {public Graph MST(Graph G, Vex s) {Map<Vex, Integer> d = new HashMap<Vex, Integer>();d.put(s, 0);PQ pq = new PQ();pq.add(s);for (Vex v : G.getVexs()) {if (!v.equals(s)) {d.put(v, Integer.MAX_VALUE);}}while(!pq.empty()){Vex u = pq.pop(); // ExtractMinfor(Vex v : G.AdjacencyList(u).keySet()){if(d.get(v) > d.get(u) + G.getw(u, v)){d.put(v, d.get(u) + G.getw(u, v));v.setValue(d.get(u) + G.getw(u, v));pq.add(v);}}}for(Vex vv : d.keySet()){System.out.println(vv.getName() + "  " + vv.getValue());}return null;}public static void main(String args[]) throws IOException{Graph g = Graph.CreateFromFile("C:/tmp/test.txt");DijkstraSeq dijk = new DijkstraSeq();dijk.MST(g, new Vex("1"));}}

 

 

 

 

 

转载于:https://www.cnblogs.com/luweiseu/archive/2012/12/11/2813801.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377020.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

sql的外键约束和主键约束_SQL约束

sql的外键约束和主键约束SQL | 约束条件 (SQL | Constraints) Constraints are the guidelines implemented on the information sections of a table. These are utilized to restrict the kind of information that can go into a table. This guarantees the precision and …

nios pio interrupt 的使能

关于nios 中的中断&#xff0c;因为要16c550中需要nios的中断环境去测试&#xff0c;所以就用到了中断。 硬件&#xff1a;在nios中添加硬件PIO,但是要使能中断功能。如下图所示&#xff1a; 系统列化&#xff0c;PIO的连接就不说了。但是要注意两地方&#xff1a;edge type&am…

《单线程的build hash table、write rows to chunks、hash join的步骤以及流程图》

Build Hash Table流程 1、初始化row buffer2、从build input table中读一行3、若读完build input table所有row&#xff0c;返回状态READING_ROW_FROM_PROBE_item4、否则&#xff0c;向hash map中写入一条row5、如果hash map 写入成功&#xff0c;返回2&#xff0c;继续执行6、…

在Scala的溪流

Scala | 流 (Scala | Streams) Stream in Scala is a type of lazy val. It is a lazy val whose elements are evaluated only when they are used in the program. Lazy initialization is a feature of Scala that increases the performance of the program. Scala中的Stre…

适合高速驱动电路的推挽电路

http://www.dzsc.com/data/html/2008-9-10/69023.html 图1是使用NPN/PNP型晶体管的互补推挽电路&#xff0c;适于驱动功率MOSFET的门极。此电路虽然具有门极电流的驱动能力&#xff0c;但射极输出波形不能比输人信号快。 图2是此电路的开关波形。它表示出tf、tr都快&#xff0c…

cholesky分解

接着LU分解继续往下&#xff0c;就会发展出很多相关但是并不完全一样的矩阵分解&#xff0c;最后对于对称正定矩阵&#xff0c;我们则可以给出非常有用的cholesky分解。这些分解的来源就在于矩阵本身存在的特殊的 结构。对于矩阵A&#xff0c;如果没有任何的特殊结构&#xff0…

socket编程常见函数使用方法

socket知识 有了IP地址&#xff0c;socket可知道是与哪一台主机的哪一个进程通信 有了端口号&#xff0c;就知道是这个进程的哪一个套接字进行传输 应用进程使用描述符与它的套接字进行通信&#xff0c;也就是说一个进程创建一个套接字时就会返回一个套接字描述符 socket的…

需求变更流程不规范,项目早晚得完蛋

很多人&#xff0c;做的项目不少&#xff0c;但成功的不多。这是一个值得深思的问题。 项目为什么这么难做&#xff1f;需求蔓延&#xff0c;客户难搞是基本原因。 如何解决上述问题&#xff1a; 1&#xff09;强化需求调研和项目设计在整个项目中的重要性 一般地&#xff0c;需…

html 表格套表格_HTML表格

html 表格套表格A table is a set of rows and columns, which could be created on a webpage in HTML, by <table> tag. The tabular representation of complex data makes it readable. 表格是一组行和列&#xff0c;可以通过<table>标签在HTML网页上创建。 复…

Android判断界面

仿造微信&#xff0c;第一次进入去引导界面&#xff0c;否则进启动界面。 package edu.hpu.init;import edu.hpu.logic.R;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.os.H…

HDU计算机网络系统2021复习提纲

目录计算机网络系统的主要功能TCP/IP模型与OSI模型的层次结构及各层功能。&#xff08;掌握&#xff09;TCP/IP参考模型各层次所对应的主要设备局域网的体系结构与IEEE.802标准数据链路层的编址方式和主要设备原理数据链路层CSMA/CD的技术原理交换机VLAN原理与划分方法数据链路…

ruby 线程id_Ruby中的线程

ruby 线程idRuby线程 (Ruby Threads) In Ruby, with the help of threads, you can implement more than one process at the same time or it can be said that Thread supports concurrent programming model. Apart from the main thread, you can create your thread with …

Dynamic web project --- AspectJ Project

本来想今天晚上 直接转到 以前的web项目 做测试。。。可惜在eclipse 添加 aspectj的时候 提示我不是 aspectj项目。。于是我就百度了好久&#xff0c;发现好多人都和我一样 &#xff0c; 不过我也发现了一些可以的 比如右键 AJDTtools --> convert to Aspectj Project ,可惜…

2013 南京邀请赛 A play the dice 求概率

1 /**2 大意&#xff1a;给定一个色子&#xff0c;有n个面&#xff0c;每一个面上有一个数字&#xff0c;在其中的m个面上有特殊的颜色&#xff0c;当掷出的色子出现这m个颜色之一时&#xff0c;可以再掷一次。。求其最后的期望3 思路&#xff1a;假设 期望为ans4 ans 1/…

掷骰子

Description: 描述&#xff1a; In this article, we are going to see a dynamic programing problem which can be featured in any interview rounds. 在本文中&#xff0c;我们将看到一个动态的编程问题&#xff0c;该问题可以在任何采访回合中体现。 Problem statement:…

《YOLO算法笔记》(草稿)

检测算法回顾 5、6年前的检测算法大体如下&#xff1a; 手动涉及特征时应该考虑的因素&#xff1a; 1、尺度不变性 2、光照不变性 3、旋转不变性 这一步骤称为特征工程&#xff0c;最重要的一个算法称为sift&#xff0c;(回顾SIFT讲解)体现了上述所有的观点。 在分类的过程中…

U盘安装Centos6.3

一 首先下载Centos6.3的光盘镜像文件&#xff0c;网上到镜像实在是太多了。 CentOS-6.3-i386-bin-DVD1.iso CentOS-6.3-i386-bin-DVD2.iso 二 下载个新版本的UltraISO, 在其菜单“启动”下有“写入硬盘镜像“功能到&#xff0c;原来用到绿色版本是8.6.2.2011不支持&#xff0c;…

[转]粵語固有辭彙與漢語北方話辭彙對照

本文转自&#xff1a;http://beta.wikiversity.org/wiki/%E7%B2%B5%E8%AA%9E%E5%9B%BA%E6%9C%89%E8%BE%AD%E5%BD%99%E8%88%87%E6%BC%A2%E8%AA%9E%E5%8C%97%E6%96%B9%E8%A9%B1%E8%BE%AD%E5%BD%99%E5%B0%8D%E7%85%A7 粵語固有辭彙與漢語北方話辭彙對照 「粵語」&#xff08;或稱「…

openlayer调用geoserver发布的地图实现地图的基本功能

转自&#xff1a;http://starting.iteye.com/blog/1039809 主要实现的功能有放大&#xff0c;缩小&#xff0c;获取地图大小&#xff0c;平移&#xff0c;线路测量&#xff0c;面积测量&#xff0c;拉宽功能&#xff0c;显示标注&#xff0c;移除标注&#xff0c;画多边形获取经…

LLVM与Codegen技术

LLVM 百度百科 LLVM是构架编译器(compiler)的框架系统&#xff0c;以C编写而成&#xff0c;用于优化以任意程序语言编写的程序的编译时间(compile-time)、链接时间(link-time)、运行时间(run-time)以及空闲时间(idle-time)&#xff0c;对开发者保持开放&#xff0c;并兼容已有…