Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.codeerror.bus;
- import android.os.Bundle;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ArrayAdapter;
- import android.widget.AutoCompleteTextView;
- import android.widget.Button;
- import android.widget.Toast;
- public class Minibus_sf extends Activity {
- AutoCompleteTextView input;
- AutoCompleteTextView inputt;
- Button btn;
- private static int count; // DECLARATION OF THE VARIABLES USED IN
- // THE PROGRAM
- static Integer n;
- static double[][] G;
- static double[] d;
- static int[] last_vertex;
- static boolean[] s;
- static int source;
- int flag = 0, flag2 = 0;
- int i = 0, j = 0;
- static int[] path;
- static int var;
- Context context = this;
- int start;
- int end;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_minibus_sf);
- btn = (Button) findViewById(R.id.btn1);
- input = (AutoCompleteTextView) findViewById(R.id.inputStart);
- inputt = (AutoCompleteTextView) findViewById(R.id.inputStop);
- final String locations[] = { "คอมเพล็ค:8", "คณะพยาบาล:8", "ประตูศรีฐาน:8", "เซ็นโทซ่า 2:8", "สถานีขนส่งแห่งที่ 1:8",
- "คณะทันตแพทย์:16", "ประตูกังสดาล:16", "ศาลจังหวัดขอนแก่น:16"};
- String locationss[] = { "a:2", "b:2", "c:2", "d:2"};
- ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line,locations);
- ArrayAdapter<String> adpp = new ArrayAdapter<String>(this,
- android.R.layout.simple_dropdown_item_1line,locationss);
- final AutoCompleteTextView input = ( AutoCompleteTextView ) this.findViewById (R.id.inputStart);
- final AutoCompleteTextView inputt = ( AutoCompleteTextView ) this.findViewById (R.id.inputStop);
- adp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- input.setThreshold(1);
- input.setAdapter ( adp );
- input.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
- start = position;
- //Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
- //TODO Do something with the selected text
- }
- });
- adpp.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
- inputt.setThreshold(1);
- inputt.setAdapter ( adpp );
- input.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
- end = position;
- //Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_LONG).show();
- //TODO Do something with the selected text
- }
- });
- btn.setOnClickListener(new OnClickListener()
- // EXECUTES WHEN THE "BUTTON" IS CLICKED
- {
- public void onClick(View v) {
- //int a = input.getSelectedItemPosition();
- //int b = inputt.getSelectedItemPosition();
- //==========================================================================
- int a = 0;
- int b = 3;
- //==========================================================================
- n = 7;
- G = new double[n + 1][n + 1];
- s = new boolean[n + 1];
- for (int x = 1; x <= n; x++)
- for (int y = 1; y <= n; y++)
- {
- if (x != y)
- G[x][y] = 4000;// +ve infinity
- else
- G[x][y] = 0;
- }
- // THESE ARE THE DISTANCES BETWEEN 2 NODES OF
- /* THE GRAPH, IF A DIRECT PATH EXISTS BETWEEN THEM */
- G[0][1] = 1.47; // VALUES WHICH CAN ALSO BE CHANGED AS PER NEEDS
- G[1][2] = 4.25;
- G[2][3] = 1.21;
- G[3][4] = 0.937;
- G[0][5] = 0.1;
- G[5][6] = 0.1;
- G[6][7] = 0.1;
- G[7][4] = 0.1;
- /*G[8][9] = 4;
- G[9][10] = 8;
- G[11][3] = 2;
- G[5][7] = 6;
- G[7][6] = 1;*/
- source = a + 1;
- int u;
- d = new double[n + 1];
- last_vertex = new int[n + 1];
- for (int i = 1; i <= n; i++) {
- s[i] = false;
- d[i] = G[source][i];
- last_vertex[i] = source;
- }
- s[source] = true;
- d[source] = 0;
- for (int i = 2; i < n; i++)
- // THE MAIN "DIJKSTRA'S" ALGORITHM
- {
- u = choseNextU();
- s[u] = true;
- for (int w = 1; w <= n; w++) {
- if ((G[u][w] != 0) && (s[w] == false)) {
- if (d[w] > d[u] + G[u][w]) {
- d[w] = d[u] + G[u][w];
- // DISTANCE BETWEEN SOURCE AND W, CAN PRINT THIS
- // TOO IF NEEDED
- last_vertex[w] = u;
- // STORES THE LAST BUT ONE VERTEX IN THE
- // SHORTEST PATH FROM SOURCE TO W
- }
- }
- }
- }
- path = new int[100]; // GETS THE PATH
- // IN THE REVERSE DIRECTION
- var = 1;
- Path(b + 1);
- path[var++] = source;
- int[] path2;
- path2 = new int[100];
- for (int i = 1; i <= var - 1; i++) {
- path2[i] = path[var - i]; // ACTUAL PATH(NON-
- // REVERSE)
- }
- String out = "Your path should be:\n" + locations[source - 1];
- for (int i = 2; i <= var - 1; i++) {
- out += " -> " + locations[path2[i] - 1];
- }
- AlertDialog.Builder alertbox = new AlertDialog.Builder(context);
- // PRINTS THE PATH IN AN ALERT BOX
- alertbox.setMessage(out);
- alertbox.setNeutralButton("Ok",
- new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface arg0, int arg1) {
- //System.exit(0);
- }
- });
- // show it
- alertbox.show();
- }
- });
- }
- static int choseNextU()
- // USED FOR GETTING THE NEXT VERTEX, WHILE COMPUTATON OF THE
- // ALGORITHM
- {
- int minu = -1;
- for (int y = 1; y <= n; y++)
- if (s[y] == false) {
- if (minu == -1) {
- if (d[y] != 0)
- minu = y;
- continue;
- } else if ((d[minu] > d[y]) && (d[y] != 0))
- minu = y;
- }
- return minu;
- }
- static void Path(int k) // GETS THE SHORTEST PATH
- {
- if (k != source) {
- path[var++] = k;
- Path(last_vertex[k]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement