Stanford Algorithms Design and Analysis Part 2 week 4

               

Problem Set-4




Programming Assignment-4

Question 1

In this assignment you will implement one or more algorithms for the all-pairs shortest-path problem. Here are data files describing three graphs: graph #1graph #2graph #3.

The first line indicates the number of vertices and edges, respectively. Each subsequent line describes an edge (the first two numbers are its tail and head, respectively) and its length (the third number). NOTE: some of the edge lengths are negative. NOTE: These graphs may or may not have negative-cost cycles.

Your task is to compute the "shortest shortest path". Precisely, you must first identify which, if any, of the three graphs have no negative cycles. For each such graph, you should compute all-pairs shortest paths and remember the smallest one (i.e., compute , where  denotes the shortest-path distance from  to ).

If each of the three graphs has a negative-cost cycle, then enter "NULL" in the box below. If exactly one graph has no negative-cost cycles, then enter the length of its shortest shortest path in the box below. If two or more of the graphs have no negative-cost cycles, then enter the smallest of the lengths of their shortest shortest paths in the box below.

OPTIONAL: You can use whatever algorithm you like to solve this question. If you have extra time, try comparing the performance of different all-pairs shortest-path algorithms!

OPTIONAL: If you want a bigger data set to play with, try computing the shortest shortest path for this graph.


import java.io.BufferedReader;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;/* * n this assignment you will implement one or more algorithms for the all-pairs shortest-path problem.  * Here are data files describing three graphs: graph #1; graph #2; graph #3.The first line indicates the number of vertices and edges, respectively. Each subsequent line describes an edge (the first two numbers are its tail and head, respectively) and its length (the third number). NOTE: some of the edge lengths are negative. NOTE: These graphs may or may not have negative-cost cycles.Your task is to compute the "shortest shortest path". Precisely, you must first identify which, if any, of the three graphs have no negative cycles. For each such graph, you should compute all-pairs shortest paths and remember the smallest one (i.e., compute minu,v∈Vd(u,v), where d(u,v) denotes the shortest-path distance from u to v).If each of the three graphs has a negative-cost cycle, then enter "NULL" in the box below. If exactly one graph has no negative-cost cycles, then enter the length of its shortest shortest path in the box below. If two or more of the graphs have no negative-cost cycles, then enter the smallest of the lengths of their shortest shortest paths in the box below.OPTIONAL: You can use whatever algorithm you like to solve this question. If you have extra time, try comparing the performance of different all-pairs shortest-path algorithms!OPTIONAL: If you want a bigger data set to play with, try computing the shortest shortest path for this graph. *  */import java.io.FileNotFoundException;public class PS4Q1 static int[][] graph; static int [][][] A; static int n; /**  * @param args  */ public static void main(String[] args) {  int count =1;  while (count <= 3 ){   FileInputStream f;   try {    f = new FileInputStream("g1.txt");    if (count == 2) f = new FileInputStream("g2.txt");    if (count == 3) f = new FileInputStream("g3.txt");    DataInputStream d = new DataInputStream(f);    BufferedReader b = new BufferedReader(new InputStreamReader(d));    n = Integer.parseInt(b.readLine().split(" ")[0]);    graph = new int [n][n];    A = new int[n][n][2];//we dont need of size [n][n][k] because at any point we refer to k and k-1 only     for (int i = 0; i < n; i++)     for (int j =0; j < n; j++)      if (i==j) graph[i][j] = 0;      else       graph[i][j] = 999999;    String str;    int x,y,z;;    while((str=b.readLine())!= null){     x = Integer.parseInt(str.split(" ")[0]);     y = Integer.parseInt(str.split(" ")[1]);     z = Integer.parseInt(str.split(" ")[2]);     graph[x-1][y-1] = z;          }    for (int i = 0; i < n; i++)     for (int j = 0 ; j < n; j++)      if (i==j) A[i][j][0] = 0;      else A[i][j][0] = graph[i][j];    for (int k = 0; k < n; k++){     for (int i = 0; i < n; i++)      for (int j = 0; j < n; j++){       A[i][j][1] = Math.min(A[i][j][0],A[i][k][0]+A[k][j][0]);             }     //copy A[][][1] to A[][][0]     for (int i = 0; i < n; i++)      for (int j = 0; j<n;j++){       A[i][j][0] = A[i][j][1];      }    }    int i = 0;    for (; i <n;i++){     if(A[i][i][0] < 0){      System.out.println(" A[i][i][0] = " + i + " " + A[i][i][0]);      System.out.println("graph " + count + " has a -ve cycle");      break;     }    }    if (i == n){     System.out.println("graph " + count + " has NO -ve cycle");     int min = 0;     for (int ix = 0; ix < n; ix++)      for (int j =0;j<n;j++){       min = Math.min(min,A[ix][j][0]);      }     System.out.println("min cost "+ min);         }   } catch (FileNotFoundException e) {    // TODO Auto-generated catch block    e.printStackTrace();   } catch (NumberFormatException e) {    // TODO Auto-generated catch block    e.printStackTrace();   } catch (IOException e) {    // TODO Auto-generated catch block    e.printStackTrace();   }   count++;  } }}



           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

Harsh Bhasin, "Algorithms: Design and Analysis" English | ISBN: 0199456666 | 2015 | 692 pages Algorithms: Design and Analysis of is a textbook designed for the undergraduate and postgraduate students of computer science engineering, information technology, and computer applications. It helps the students to understand the fundamentals and applications of algorithms. The book has been divided into four sections: Algorithm Basics, Data Structures, Design Techniques and Advanced Topics. The first section explains the importance of algorithms, growth of functions, recursion and analysis of algorithms. The second section covers the data structures basics, trees, graphs, sorting in linear and quadratic time. Section three discusses the various design techniques namely, divide and conquer, greedy approach, dynamic approach, backtracking, branch and bound and randomized algorithms used for solving problems in separate chapters. The fourth section includes the advanced topics such as transform and conquer, decrease and conquer, number thoeretics, string matching, computational geometry, complexity classes, approximation algorithms, and parallel algorithms. Finally, the applications of algorithms in Machine Learning and Computational Biology areas are dealt with in the subsequent chapters. This section will be useful for those interested in advanced courses in algorithms. The book also has 10 appendixes which include topics like probability, matrix operations, Red-black tress, linear programming, DFT, scheduling, a reprise of sorting, searching and amortized analysis and problems based on writing algorithms. The concepts and algorithms in the book are explained with the help of examples which are solved using one or more methods for better understanding. The book includes variety of chapter-end pedagogical features such as point-wise summary, glossary, multiple choice questions with answers, review questions, application-based exercises to help readers test their understanding of the learnt concepts.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值