DijkstrasAlgorithmforSingle-SourceShortestPathProblem.pptVIP

  • 2
  • 0
  • 约2.82千字
  • 约 34页
  • 2017-05-05 发布于湖北
  • 举报

DijkstrasAlgorithmforSingle-SourceShortestPathProblem.ppt

DijkstrasAlgorithmforSingle-SourceShortestPathProblem

Dijkstra’s Algorithm for Single-Source Shortest Path Problem;Outline;Point-to-point Shortest Path Problem;Point-to-point Shortest Path Problem;Dijkstra’s Idea;;To extract path;To get minimal spanning tree;Example Application;Movement Rules ;Movement rules;Read problem again.;Designing a Java implementation;Edge representation;Simple special case;for (int x = 0; x V; x++) { switch (x % n) { case 0: if (x-n = 0) neighbor[x][0] = x-n; // N if (x+n V) neighbor[x][1] = x+n; // S break; case 1: if ((x-n = 0) (x % n n-1)) neighbor[x][0] = x-n+1; // NE if ((x+n N) (x % n 0)) neighbor[x][1] = x+n-1; // SW ...etc. }};Alternatives;How to represent settled?;How to represent distances?;How to represent Q?;ComparatorInteger shortestDistance = new ComparatorInteger() { public int compare(Integer L, Integer R) { if (d[L] d[R]) return 1; if (d[L] d[R]) return -1; if (L R) return 1; if (L R) return -1; return 0; } }; PriorityQueueInteger q = new PriorityQueueInteger(N, shortestDistance); ;A literal coding of abstract algorithm; // while (Q ≠ ?) { while (! q.isEmpty) { // choose x ? Q to minimize d(x); Q = Q – {x}; x = q.poll(); if (x==dest) break; // settled = settled U {x}; settled[x] = true; // for each unsettled neighbor y of x { for (int i = 0; i 2; i++) { y = neighbor[x][i]; if ((i != -1) ! settled[y]) { // if (d(y)d(x) + len(x,y)) { if (d[y]d[x] + 1){ // d(y) = d(x)+ len(x,y); d[y] = d[x]+1; // back(y) = x; back[y] = x; }}}};Q details; while (! q.isEmpty) { x = q.poll(); if (x==dest) break; settled[x] = true; for (int i = 0; i 2; i++) { y = neighbor[x][i]; if ((i != -1) ! settled[y]) { if (d[y]d[x] + 1){ d[y] = d[x]+1; back[y] = x;

文档评论(0)

1亿VIP精品文档

相关文档