0% found this document useful (0 votes)
269 views16 pages

19Nh14 102190051 Lab13 Chương Trình MapReduce Shortest Path Using Parallel Breadth First Search BFS 02

This document describes using MapReduce to solve the single-source shortest path problem using parallel breadth-first search in an iterative manner. It includes the MapReduce algorithm, input data format, and code to implement the algorithm in Java for Hadoop. The algorithm finds the shortest paths from a source node to all other nodes in the graph stored in a text file. It iterates the MapReduce job until convergence is reached when no more shortest path distances can be improved.

Uploaded by

Tri An Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views16 pages

19Nh14 102190051 Lab13 Chương Trình MapReduce Shortest Path Using Parallel Breadth First Search BFS 02

This document describes using MapReduce to solve the single-source shortest path problem using parallel breadth-first search in an iterative manner. It includes the MapReduce algorithm, input data format, and code to implement the algorithm in Java for Hadoop. The algorithm finds the shortest paths from a source node to all other nodes in the graph stored in a text file. It iterates the MapReduce job until convergence is reached when no more shortest path distances can be improved.

Uploaded by

Tri An Nguyễn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

XỬ LÝ PHÂN TÁN MAPREDUCE

Single-source all pairs shortest path using MapReduce using parallel Breadth-
First Search (BFS)

1. Thuật toán MapReduce


In this problem we are solving the single-source all pairs shortest path using
MapReduce using parallel Breadth-First Search (BFS) in an iterative manner. The source
node is '1'. The source node is processed first, then the nodes connected to the source node
are processed and so on.
Input format : source<tab>adjacency_list:distance_from_the_source:
Termination Condition: Check the output value with the previous input if any of the
distance is still infinity(125 in our code) the continue else convergence is reached and stop
Algorithm:
Step1: Input file having structure as node id and corresponding adjacency list
Step2: Mapper read the file line by line and emit distance of corresponding adjacency
list nodes from this node.
Step3: Reducer find the minimum of the distance of all the distances of a given node.
Step4: This output file is again read and stored in the map function to check if
convergence is reached.
if convergence is reached
stop;
else repeat step1 with this output file as input file.
1.1 Tổ chức dữ liệu đầu vào
File input.txt
1 0 2:
2 125 3:
3 125 2:13:
4 125 14:
5 125 14:
6 125 7:
7 125 6:8:17:
8 125 7:20:
9 125 10:
10 125 9:20:
11 125 12:
12 125 11:13:22:
13 125 3:12:14:
14 125 4:5:13:15:
15 125 14:16:
16 125 15:27:
17 125 7:27:
18 125 19:35:
19 125 18:20:21:
20 125 8:10:19:
21 125 19:
22 125 12:24:
23 125 32:
24 125 22:32:
25 125 26:32:
26 125 25:27:
27 125 16:17:26:28:33:
28 125 27:44:
29 125 37:38:
30 125 45:
31 125 32:
32 125 23:24:25:31:40:41:46:
33 125 27:42:43:
34 125 49:
35 125 18:50:
36 125 50:
37 125 29:
38 125 29:45:53:
39 125 45:
40 125 32:55:
41 125 32:47:
42 125 33:
43 125 33:44:48:
44 125 28:43:52:
45 125 30:38:39:61:
46 125 32:56:60:
47 125 41:
48 125 43:
49 125 34:50:71:
50 125 35:36:49:58:
51 125 58:
52 125 44:
53 125 38:62:
54 125 66:
55 125 40:
56 125 46:68:
57 125 43:69:
58 125 50:51:72:
59 125 69:
60 125 46:66:67:
61 125 45:65:
62 125 53:63:64:
63 125 62:
64 125 62:
65 125 61:80:
66 125 54:60:79:
67 125 60:
68 125 56:77:78:
69 125 57:59:77:
70 125 82:102:
71 125 49:72:
72 125 58:71:73:74:75:
73 125 72:
74 125 72:
75 125 72:76:104:
76 125 75:
77 125 68:69:82:98:
78 125 68:81:
79 125 66:80:81:
80 125 65:79:89:
81 125 78:79:95:
82 125 70:77:101:
83 125 85:
84 125 85:
85 125 83:84:86:
86 125 85:87:
87 125 86:88:
88 125 87:89:91:
89 125 80:88:90:
90 125 89:93:
91 125 88:92:
92 125 91:93:
93 125 90:92:94:
94 125 93:95:96:
95 125 81:94:
96 125 94:
97 125 99:
98 125 77:99:
99 125 97:98:100:
100 125 99:107:
101 125 82:105:
102 125 70:103:
103 125 102:104:105:
104 125 75:103:106:
105 125 101:103:107:
106 125 104:108:
107 125 100:105:
108 125 106:109:110:
109 125 108:
110 125 108:

2. Chương trình MapReduce


//File DijikstraAlgo.java

import java.io.IOException;
import java.util.*;
import java.io.*;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapred.*;
import org.apache.hadoop.mapred.Reducer;

public class DijikstraAlgo {


public static String OUT = "outfile";
public static String IN = "inputlarger";

public static class Map extends MapReduceBase implements


Mapper<LongWritable, Text, LongWritable, Text> {
@Override
public void map(LongWritable key, Text value,
OutputCollector<LongWritable, Text> output, Reporter reporter) throws IOException {
Text word = new Text();
String line = value.toString(); // looks like 1 0 2:3:
String[] sp = line.split("\t| "); // splits on space
int distanceadd = Integer.parseInt(sp[1]) + 1;
String[] PointsTo = sp[2].split(":");
for (int i = 0; i < PointsTo.length; i++) {
word.set("VALUE " + distanceadd); // tells me to look at
distance value
output.collect(new
LongWritable(Integer.parseInt(PointsTo[i])), word);
word.clear();
}

// pass in current node's distance (if it is the lowest distance)


word.set("VALUE " + sp[1]);
output.collect(new LongWritable(Integer.parseInt(sp[0])), word);
word.clear();

word.set("NODES " + sp[2]);


output.collect(new LongWritable(Integer.parseInt(sp[0])), word);
word.clear();
}
}//Map

public static class Reduce extends MapReduceBase implements Reducer


<LongWritable, Text, LongWritable, Text> {
@Override
public void reduce(LongWritable key, Iterator<Text> values,
OutputCollector<LongWritable, Text> output, Reporter reporter)throws IOException {
String nodes = "UNMODED";
Text word = new Text();
int lowest = 125; // In this 125 is considered as
infinite distance
while (values.hasNext()) { // looks like NODES/VALUES 1 0
2:3:, we need to use the first as a key
String[] sp = values.next().toString().split(" "); // splits
on space
// look at first value
if (sp[0].equalsIgnoreCase("NODES")) {
nodes = null;
nodes = sp[1];
} else if (sp[0].equalsIgnoreCase("VALUE")) {
int distance = Integer.parseInt(sp[1]);
lowest = Math.min(distance, lowest);
}
}
word.set(lowest + " " + nodes);
output.collect(key, word);
word.clear();
}
}//Reduce

public static void run(String[] args) throws Exception {


IN = args[0];
OUT = args[1];
String input = IN;
String output = OUT + System.nanoTime();
boolean isdone = false;

// Reiteration again and again till the convergence


while (isdone == false) {
JobConf conf = new JobConf(DijikstraAlgo.class);
conf.setJobName("Dijikstra");
conf.setOutputKeyClass(LongWritable.class);
conf.setOutputValueClass(Text.class);
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);

FileInputFormat.setInputPaths(conf, new Path(input));


FileOutputFormat.setOutputPath(conf, new Path(output));

JobClient.runJob(conf);
input = output + "/part-00000";
isdone = true;// set the job to NOT run again!
Path ofile = new Path(input);
FileSystem fs = FileSystem.get(new Configuration());
BufferedReader br = new BufferedReader(new
InputStreamReader(fs.open(ofile)));
HashMap<Integer, Integer> imap = new HashMap<Integer, Integer>();
String line = br.readLine();

// Read the current output file and put it into HashMap


while (line != null) {
String[] sp = line.split("\t| ");
int node = Integer.parseInt(sp[0]);
int distance = Integer.parseInt(sp[1]);
imap.put(node, distance);
line = br.readLine();
}
br.close();

// Check for convergence condition if any node is still left then


continue else stop
Iterator<Integer> itr = imap.keySet().iterator();
while (itr.hasNext()) {
int key = itr.next();
int value = imap.get(key);
if (value >= 125) {
isdone = false;
}
}
input = output;
output = OUT + System.nanoTime();
}
}

public static void main(String[] args) throws Exception {


run(args);
}
}
3. Biên dịch và chạy chương trình MapReduce
▪ Mở cửa sổ lệnh dưới quyền Administrator
▪ Khởi động các tiến trình hadoop: chạy start-all.cmd
▪ Chuyển vào thư mục hiện hành
cd C:\hadoop\MapReduceDijkstra

▪ Khai báo biến môi trường chỉ đường dẫn


set HADOOP_LIB=c:\hadoop\hadoop-2.6.0\share\hadoop
set HADOOP_CLASSPATH=%HADOOP_LIB%\mapreduce\hadoop-
mapreduce-client-core-2.6.0.jar;%HADOOP_LIB%\common\hadoop-common-
2.6.0.jar;

▪ Biên dịch mã nguồn java thành các file classes\*.class


C:\hadoop\MapReduceDijkstra> md classes
C:\hadoop\MapReduceDijkstra> javac -classpath %HADOOP_CLASSPATH%
-d classes *.java
C:\hadoop\MapReduceDijkstra> dir classes

▪ Xóa file *.jar nếu đã có trước đó


C:\hadoop\MapReduceDijkstra> del DijikstraAlgoXYZ.jar

▪ Đóng gói file *. jar


C:\hadoop\MapReduceDijkstra> jar -cvf DijikstraAlgoXYZ.jar -C classes .

▪ Xóa thư mục /inputXYZ nếu đã có, tạo mới và chép dữ liệu vào
C:\hadoop\MapReduceDijkstra> hadoop fs -rm -r /inputXYZ
C:\hadoop\MapReduceDijkstra> hadoop fs -mkdir /inputXYZ
C:\hadoop\MapReduceDijkstra> hadoop fs -put input.txt /inputXYZ
C:\hadoop\MapReduceDijkstra> hadoop fs -ls /inputXYZ

▪ Xóa thư mục /outputXYZ nếu đã có


C:\hadoop\MapReduceDijkstra> hadoop fs -rm –r /outputXYZ

▪ Chạy chương trình Mapreduce với dữ liệu trong thư mục /inputXYZ, kết quả xuất
ra /outputXYZ
C:\hadoop\MapReduceDijkstra> hadoop jar DijikstraAlgoXYZ.jar
DijikstraAlgo /inputXYZ /outputXYZ
▪ Xem kết quả trong /outputXYZ
C:\hadoop\MapReduceDijkstra> hadoop fs -ls /outputXYZ
C:\hadoop\MapReduceDijkstra> hadoop fs -cat /outputXYZ/part-00000
C:\hadoop\MapReduceDijkstra> hadoop fs -ls /
...
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:42
/outputXYZ8410063875399
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:43
/outputXYZ8438998593800
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:43
/outputXYZ8466858608600
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:44
/outputXYZ8493736824100
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:44
/outputXYZ8521702985899
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:45
/outputXYZ8548641333699
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:45
/outputXYZ8575511550900
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:46
/outputXYZ8602344027600
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:46
/outputXYZ8630000939199
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:47
/outputXYZ8656867697199
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:47
/outputXYZ8684539938899
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:47
/outputXYZ8712262906899
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:48
/outputXYZ8738960567900
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:48
/outputXYZ8766829451500
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:49
/outputXYZ8793523778799
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:49
/outputXYZ8821352860500
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:50
/outputXYZ8848041850900
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:50
/outputXYZ8875991420900
drwxr-xr-x - Administrator supergroup 0 2020-05-29 21:51
/outputXYZ8903077597800

C:\hadoop\MapReduceDijkstra>hadoop fs -ls /outputXYZ8903077597800


Found 2 items
-rw-r--r-- 1 Administrator supergroup 0 2020-05-29 21:51
/outputXYZ8903077597800/_SUCCESS
-rw-r--r-- 1 Administrator supergroup 1458 2020-05-29 21:51
/outputXYZ8903077597800/part-00000
C:\hadoop\MapReduceDijkstra>hadoop fs -cat
/outputXYZ8903077597800/part-00000
1 0 2:
2 1 3:
3 2 2:13:
4 5 14:
....
109 19 108:
110 19 108:
C:\hadoop\MapReduceDijkstra>

4. Bài tập
Mô tả quá trình thực hiện sử dụng giải thuật MapReduce theo các bước như sau:
a) Tổ chức dữ liệu đầu vào
b) Quá trình Map? Cho ví dụ minh họa
c) Quá trình Reduce? Cho ví dụ minh họa
d) Chạy chương trình với nhiều bộ dữ liệu khác.

5. Thuật toán Dijkstra sử dụng MapReduce

Bước 1: Khởi tạo


Với đỉnh v V, gọi nhãn d[v] là độ dài đường đi ngắn nhất từ s tới v. Ta sẽ tính
các d[v]. Ban đầu d[v] được khởi gán bằng w[s, v]. Nhãn của mỗi đỉnh có hai trạng
thái tự do hay cố định, nhãn tự do có nghĩa là có thể còn tối ưu hơn được nữa và
nhãn cố định tức là d[v] đã bằng độ dài đường đi ngắn nhất từ s tới v nên không thể
tối ưu thêm. Để làm điều này ta có thể sử dụng kỹ thuật đánh dấu: Free[v] = TRUE
hay FALSE tuỳ theo d[v] tự do hay cố định. Ban đầu các nhãn đều tự do.
Bước 2: Lặp
Cố định nhãn: Chọn trong các đỉnh có nhãn tự do, lấy ra đỉnh u là đỉnh
có d[u] nhỏ nhất và cố định nhãn đỉnh u.
Sửa nhãn: Dùng đỉnh u, xét tất cả những đỉnh v và sửa lại các d[v] theo công
thức: d[v] := min(d[v], d[u] + c[u, v])
Bước lặp sẽ kết thúc khi mà đỉnh đích t được cố định nhãn (tìm được đường
đi ngắn nhất từ s đến t); hoặc tại thao tác cố định nhãn, tất cả các đỉnh tự do đều
có nhãn là + (không tồn tại đường đi).
Có thể đặt câu hỏi, ở thao tác 1, tại sao đỉnh u như vậy được cố định nhãn, giả
sử d[u] còn có thể tối ưu thêm được nữa thì tất phải có một đỉnh t mang nhãn tự do
sao cho d[u] > d[t] + c[t, u]. Do trọng số c[t, u] không âm nên d[u] > d[t], trái với
cách chọn d[u] là nhỏ nhất. Tất nhiên trong lần lặp đầu tiên thì S là đỉnh được cố
định nhãn do d[s] = 0.
Bước 3: Kết hợp với việc lưu vết đường đi trên từng bước sửa nhãn, thông báo
đường đi ngắn nhất tìm được hoặc cho biết không tồn tại đường đi (d[t]
= +).
Có thể mô tả ngắn gọn thuật toán bằng giả mã như sau:
Bước 1: d[s] := 0 ;
d[v] := + ( v  V \{s});
u:= s;
Bước 2: Lặp nếu u ≠ t (với u  S)
2.1. Nếu d[v] > d[u] + c[u,v] thì
d[v] := min{d[v], d[u] + c[u, v]}
trace[v]:=u(với vS - tập các đỉnh đã tối ưu)
2.2. Chọn v có d[v] nhỏ nhất //v=0 → không có đường đi.
2.3. Nếu v ≠ 0 thì thêm v vào S; u:= v
Bước 3: In ra đường đi tối ưu từ s đến t hoặc thông báo
vô nghiệm.
----------------------------------------------------------

You might also like