Advertisement
Guest User

Untitled

a guest
Jun 3rd, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.79 KB | None | 0 0
  1. package com.codeerror.bus;
  2.  
  3. import android.os.Bundle;
  4. import android.app.Activity;
  5. import android.app.AlertDialog;
  6. import android.content.Context;
  7. import android.content.DialogInterface;
  8. import android.view.View;
  9. import android.view.View.OnClickListener;
  10. import android.widget.AdapterView;
  11. import android.widget.AdapterView.OnItemClickListener;
  12. import android.widget.ArrayAdapter;
  13. import android.widget.AutoCompleteTextView;
  14. import android.widget.Button;
  15. import android.widget.Toast;
  16.  
  17. public class Minibus_sf extends Activity {
  18.  
  19.  
  20. AutoCompleteTextView input;
  21. AutoCompleteTextView inputt;
  22. Button btn;
  23. private static int count; // DECLARATION OF THE VARIABLES USED IN
  24. // THE PROGRAM
  25. static Integer n;
  26. static double[][] G;
  27. static double[] d;
  28. static int[] last_vertex;
  29. static boolean[] s;
  30. static int source;
  31. int flag = 0, flag2 = 0;
  32. int i = 0, j = 0;
  33. static int[] path;
  34. static int var;
  35. Context context = this;
  36.  
  37. int start;
  38. int end;
  39.  
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_minibus_sf);
  44. btn = (Button) findViewById(R.id.btn1);
  45.  
  46. input = (AutoCompleteTextView) findViewById(R.id.inputStart);
  47. inputt = (AutoCompleteTextView) findViewById(R.id.inputStop);
  48.  
  49.  
  50. final String locations[] = { "คอมเพล็ค:8", "คณะพยาบาล:8", "ประตูศรีฐาน:8", "เซ็นโทซ่า 2:8", "สถานีขนส่งแห่งที่ 1:8",
  51. "คณะทันตแพทย์:16", "ประตูกังสดาล:16", "ศาลจังหวัดขอนแก่น:16"};
  52.  
  53. String locationss[] = { "a:2", "b:2", "c:2", "d:2"};
  54.  
  55. ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
  56. android.R.layout.simple_dropdown_item_1line,locations);
  57. ArrayAdapter<String> adpp = new ArrayAdapter<String>(this,
  58. android.R.layout.simple_dropdown_item_1line,locationss);
  59.  
  60.  
  61.  
  62. final AutoCompleteTextView input = ( AutoCompleteTextView ) this.findViewById (R.id.inputStart);
  63. final AutoCompleteTextView inputt = ( AutoCompleteTextView ) this.findViewById (R.id.inputStop);
  64.  
  65. adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  66.  
  67. input.setThreshold(1);
  68. input.setAdapter ( adp );
  69.  
  70. input.setOnItemClickListener(new OnItemClickListener() {
  71. public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
  72. start = position;
  73. //Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
  74. //TODO Do something with the selected text
  75. }
  76. });
  77.  
  78. adpp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
  79. inputt.setThreshold(1);
  80. inputt.setAdapter ( adpp );
  81. input.setOnItemClickListener(new OnItemClickListener() {
  82. public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
  83. end = position;
  84. //Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
  85. //TODO Do something with the selected text
  86. }
  87. });
  88.  
  89. btn.setOnClickListener(new OnClickListener()
  90. // EXECUTES WHEN THE "BUTTON" IS CLICKED
  91. {
  92.  
  93. public void onClick(View v) {
  94. //int a = input.getSelectedItemPosition();
  95. //int b = inputt.getSelectedItemPosition();
  96.  
  97. //==========================================================================
  98. int a = 0;
  99. int b = 3;
  100. //==========================================================================
  101.  
  102. n = 7;
  103.  
  104. G = new double[n + 1][n + 1];
  105. s = new boolean[n + 1];
  106. for (int x = 1; x <= n; x++)
  107. for (int y = 1; y <= n; y++)
  108.  
  109. {
  110. if (x != y)
  111. G[x][y] = 4000;// +ve infinity
  112. else
  113. G[x][y] = 0;
  114. }
  115.  
  116. // THESE ARE THE DISTANCES BETWEEN 2 NODES OF
  117. /* THE GRAPH, IF A DIRECT PATH EXISTS BETWEEN THEM */
  118.  
  119. G[0][1] = 1.47; // VALUES WHICH CAN ALSO BE CHANGED AS PER NEEDS
  120. G[1][2] = 4.25;
  121. G[2][3] = 1.21;
  122. G[3][4] = 0.937;
  123.  
  124. G[0][5] = 0.1;
  125. G[5][6] = 0.1;
  126. G[6][7] = 0.1;
  127. G[7][4] = 0.1;
  128.  
  129. /*G[8][9] = 4;
  130. G[9][10] = 8;
  131. G[11][3] = 2;
  132. G[5][7] = 6;
  133. G[7][6] = 1;*/
  134. source = a + 1;
  135. int u;
  136. d = new double[n + 1];
  137. last_vertex = new int[n + 1];
  138. for (int i = 1; i <= n; i++) {
  139.  
  140. s[i] = false;
  141. d[i] = G[source][i];
  142. last_vertex[i] = source;
  143. }
  144.  
  145. s[source] = true;
  146. d[source] = 0;
  147. for (int i = 2; i < n; i++)
  148.  
  149. // THE MAIN "DIJKSTRA'S" ALGORITHM
  150. {
  151. u = choseNextU();
  152. s[u] = true;
  153. for (int w = 1; w <= n; w++) {
  154. if ((G[u][w] != 0) && (s[w] == false)) {
  155. if (d[w] > d[u] + G[u][w]) {
  156. d[w] = d[u] + G[u][w];
  157. // DISTANCE BETWEEN SOURCE AND W, CAN PRINT THIS
  158. // TOO IF NEEDED
  159. last_vertex[w] = u;
  160. // STORES THE LAST BUT ONE VERTEX IN THE
  161. // SHORTEST PATH FROM SOURCE TO W
  162. }
  163. }
  164. }
  165. }
  166. path = new int[100]; // GETS THE PATH
  167. // IN THE REVERSE DIRECTION
  168. var = 1;
  169. Path(b + 1);
  170. path[var++] = source;
  171. int[] path2;
  172. path2 = new int[100];
  173. for (int i = 1; i <= var - 1; i++) {
  174. path2[i] = path[var - i]; // ACTUAL PATH(NON-
  175. // REVERSE)
  176. }
  177. String out = "Your path should be:\n" + locations[source - 1];
  178. for (int i = 2; i <= var - 1; i++) {
  179. out += " -> " + locations[path2[i] - 1];
  180. }
  181. AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
  182. // PRINTS THE PATH IN AN ALERT BOX
  183. alertbox.setMessage(out);
  184. alertbox.setNeutralButton("Ok",
  185. new DialogInterface.OnClickListener() {
  186. public void onClick(DialogInterface arg0, int arg1) {
  187. //System.exit(0);
  188. }
  189. });
  190. // show it
  191. alertbox.show();
  192. }
  193.  
  194. });
  195. }
  196.  
  197. static int choseNextU()
  198. // USED FOR GETTING THE NEXT VERTEX, WHILE COMPUTATON OF THE
  199. // ALGORITHM
  200. {
  201. int minu = -1;
  202. for (int y = 1; y <= n; y++)
  203. if (s[y] == false) {
  204. if (minu == -1) {
  205. if (d[y] != 0)
  206. minu = y;
  207. continue;
  208. } else if ((d[minu] > d[y]) && (d[y] != 0))
  209. minu = y;
  210. }
  211. return minu;
  212. }
  213.  
  214. static void Path(int k) // GETS THE SHORTEST PATH
  215. {
  216. if (k != source) {
  217. path[var++] = k;
  218. Path(last_vertex[k]);
  219. }
  220. }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement