1. 程式人生 > >I fight, so you don't have to

I fight, so you don't have to

單源最短路徑問題

是指從一個指定頂點s到其他所有頂點i之間的距離,因為是單一頂點到其他頂點的距離,所以稱為單源
       設圖G<V,E>是一個有向加權網路,其中VE分別為頂點集合和邊集合,其邊權鄰接矩陣為W,邊上權值w(i,j)>0,i,j∈VV={0,1,…,N-1}。
       設dist(i)為最短的路徑長度,即距離,其中s∈V且i≠s。這裡我是學習的Dijkstra演算法,並將其並行化。

最短路徑序列演算法

       Dijkstra演算法是單源最短路徑問題的經典解法之一,還有很多單源最短路徑的演算法,比如Bellman-ford、spfa,下面說說Dijkstra演算法的基本思想:
  假定有一個待搜尋的頂點表VL

,初始化時做:dist(s)<-0,dist(i)=∞(i≠s),VL=V。每次從VL(非空集)中選取這樣的一個頂點u,它的dist(u)最小。將選出的u點作為搜尋頂點,對於其他還在VL內的頂點v,若

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>

using namespace std;
const int maxn = 10000;
int dist[maxn];
int mp[maxn][maxn];
bool
vis[maxn]; int n,m; const int oo = INT_MAX; void Dijkstra(int s) { for(int i=1; i<=n; i++) { dist[i] = mp[s][i]; vis[i]=false; } dist[s] = 0; vis[s] = true; int u; for(int i=2; i<=n; i++) { int minn = oo; for(int j=1; j<=n; j++) if
(!vis[j]&&minn>dist[j]) { minn = dist[j]; u=j; } vis[u]=true; for(int j=1; j<=n; j++) if(!vis[j]&&mp[u][j]<oo) { if(dist[u]+mp[u][j]<dist[j]) dist[j]=dist[u]+mp[u][j]; } } } int main() { for(int i=0; i<maxn; i++) for(int j=0; j<maxn; j++) { if(i!=j)mp[i][j]=oo; else mp[i][j]=0; } scanf("%d%d",&n,&m); for(int i=0; i<m; i++) { int s,t,len; scanf("%d%d%d",&s,&t,&len); mp[s][t]=len; } int st; scanf("%d",&st); Dijkstra(st); for(int i=1; i<=n; i++) printf("%d ",dist[i]); puts(""); return 0; }

以下這段程式碼我們可以進行並行化,每個處理器分派n = N/p(注意這裡的n跟我程式中寫的n不是一個n)個節點進行初始化

    for(int i=1; i<=n; i++)
    {
        dist[i] = mp[s][i];
        vis[i]=false;
    }
    dist[s] = 0;

下面這段程式碼可以並行化為:首先每個處理器分派n個節點分別求區域性最小值,然後再p個處理器合作求全域性最小值,最後再將這一最小值廣播出去。p個處理器合作方法如下:當p為偶數時,後p/2個處理器將自己的區域性最小值分別傳送到對應的前p/2個處理器中,由前p/2個處理器比較出2個區域性最小值中相對較小者並保留。當p為奇數時,設p=2h+1,則後h個處理器的值分別傳送到前h個處理器中,比較並保留當前最小值。一次這樣的迴圈過後,p個最小值減少了一半,兩次後變為1/4,如此一層一層的比較,logp次迴圈後,就可以得出唯一的全域性最小值。

for(int j=1; j<=n; j++)
            if(!vis[j]&&minn>dist[j])
            {
                minn = dist[j];
                u=j;
            }

下面的這段程式碼,可以每個處理器分配n個頂點,然後獨立進行更新dist的操作。

        for(int j=1; j<=n; j++)
            if(!vis[j]&&mp[u][j]<oo)
            {
                if(dist[u]+mp[u][j]<dist[j])
                    dist[j]=dist[u]+mp[u][j];
            }

根據以上思路,最短路徑的並行演算法就很明瞭了,使用了p個處理器,那麼時間複雜度就是O(N^2/p+Nlogp)。
我們能夠進行並行的方法就比較多了,可以用openmp或者mpi等等。最近在學習mpi並行程式設計,學的還不是很6,具體並行的程式就不貼啦。

相關推薦

I fight, so you don't have to

單源最短路徑問題 是指從一個指定頂點s到其他所有頂點i之間的距離,因為是單一頂點到其他頂點的距離,所以稱為單源。        設圖G<V,E>是一個有向加權網路,其中V和E分別為頂點集合和邊集合,其邊權鄰接矩陣為W,邊上權值w(i,j)>

No, You Don't Have to Locate Your Startup in the Bay Area

When Alexis Ohanian met Jewel Burks a few years back while filming a show called Small Empires, he was pretty clear about how formidable he thought the you

WampServer出現You dont have permission to access/on this server提示

ron 服務 mps all -m 如何 onf sta highlight WampServer出現You don’t have permission to access/on this server提示 本地搭建WampServer,輸入http://127.0.0

Wireshark 抓包遇到 you dont have permission to capture on that device mac 錯誤的解決方案

打開 min 遇到 分享 hone eas watermark tail 錯誤 Wireshark 抓包遇到 you don’t have permission to capture on that device mac 錯誤的解決方案 上次有篇博客講了如何利用wires

mac osx下apache下的坑: you dont have permission to access / on this server

pos require tor director div http localhost mce pan 在Mac下Apache修改默認站點的目錄時,遇到403錯誤, you don’t have permission to access / on this server

apache2.4 You dont have permission to access / on

mission index ESS per 2.4 system win follow mis apache2.4 You don‘t have permission to access / on this server. 在配置完apache2.4conf/extra/h

PHPstudy 區域網其他主機訪問403(You don`t have permission to......)

PHPstudy 區域網其他主機訪問403(You don`t have permission to…) DVWA這個測試環境以前裝在Apache上,但是Apache複雜的配置環境實在是對新手不友好,所以換成了PHPstudy(不得不說,確實很方便,但也是偶爾存

apache2.4 報Forbidden You don't have permission to access / on this server.

檢視apache版本D:\phpStudy\PHPTutorial\Apache\bin>httpd -v Server version: Apache/2.4.23 (Win32) Server

wamp配置出現You don't have permission to access / on this server.

<Directory />     Options FollowSymLinks     AllowOverride None     Order deny,allow     Allow from all </Directory>

apache 2.4.23 只能本地訪問,其他使用者不能訪問,提示You don't have permission to access

  這個版本的httpd.conf的配置方法跟原版本的設定不一樣了。   需要在目錄安全配置中 修改為 Require all granted   比如  把Require local 修改為Require all granted. 特別要注意,需將httpd-vhosts

wamp出現You dont have permission to access/on this server提示

本地搭建wamp,輸入http://127.0.0.1訪問正常,當輸入http://localhost/,apache出現You don't have permission to access/on this server.的提示,如何解決?

WAMPServer: You dont have permission to access /phpmyadmin on this server.

WAMPServer: You don’t have permission to access /phpmyadmin on this server. apache_error.log日誌錯誤: AH01630: client denied by server configura

Xcode6:The file couldn’t be opened because you dont have permission to view it

1、前言 最近為了相容iOS8升級到Xcode6.0編譯之前的工程,結果App無法在真機上執行。報錯如下:The file “xxxx.app” couldn’t be opened because you don’t have permission to view it

解決mac os You don't have permission to access / on this server(如果你嘗試了很多辦法還不行可以試試這個)

  其實如果自己 DocumentRoot "/apache/webRoot" <Directory "/apache/webRoot"> 這兩個都改了之後發現還是forbidden,那應該就不是什麼directory問題了,這時候有兩種情況: 如果你那個目

iOS 報錯:沒有檢視許可權the file "xxx.app" couldn't be opened because you don't have permission to view it.

iOS the file "xxx.app" couldn't be opened because you don't have permission to view it.問題解決方法彙總 (1) 最近重構專案,增加target來區分環境進行差異化編譯配置。 報

php多站點配置以及Forbidden You don't have permission to access / on this server問題解決

前幾天在電腦上配置多站點的後突然出現了:”You don’t have permission to access / on this server!“ 究其原因是:swampserver預設訪問的是www資料夾的站點,不允許訪問其他的站點。花了老

安裝wamp 解決IIS埠衝突You don't have permission to access / on this serve

# wamp - 安裝wamp 1. 下載 2. 一定要裝vc++2012 因為phh 2.5以上依賴。不然可能會遇到缺少mvc110.dll 如果出現安裝VC++之後解除安裝重灌 3.

iOS The file “XueBa.app” couldn’t be opened because you dont have permission to view it.

(1) 最近重構專案,增加target來區分環境進行差異化編譯配置。 報錯the file "xxx.app" couldn't be opened because you don't have permission to view it. 後發現 對

You don't have permission to access / on this server. --->解決方法

有的時候,比如做Apache專案時(Hudson),你或許會遇到 You don't have permission to access / on this server. 錯誤,提示你沒有許可權訪問,這個時候 你除了check 777 許可權外(chmod 777 xxx

iOS the file "xxx.app" couldn't be opened because you don't have permission to view it.

最近重構專案,增加target來區分環境進行差異化編譯配置。 報錯the file "xxx.app" couldn't be opened because you don't have permission to view it. 後發現 對應target的in