Bus Transfer Algorithm

Source: Internet
Author: User

Even the bus transfer algorithm written in March:
Bus Transfer one-stop algorithm bustransfer
/**
* Algorithm idea of bus transfer to one station:
* (Note: Vehicle Information, site information, and bus information are equivalent to storing information in the form of hashmap)
* 1. Obtain all bus information from the database and store it to the arraylist. Each specific information has three metadata items:
* Number of bus times, bus stations, and bus stations that are located at the origin site of the bus times. Save the specific information with hashmap
* 2. Then, the bus information data is structured, all bus stations are extracted, and all the trains corresponding to each station are extracted.
* In one-to-one correspondence, a single vehicle count information is stored in hashmap, and all vehicle count information corresponding to the site is stored in arraylist,
* All trains on the site are stored in hashmap.
* 3. Based on the query requirements, find the structured public transport information data, all the trains that pass through the departure location, and pass through the destination
* All trains are traversed separately to filter out the transit sites that meet the requirements. The filtering rule is:
* When the site is located, the number of origin sites of the site to which the site is located. If the number of sites is smaller than the start time of the site to which the site is located
* If the number of sending sites meets the rules, the site information is saved to the arraylist that matches the site, and vice versa.
* 4. Obtain the transit site information of the departure location and destination in the query conditions respectively (the transit site information is stored in the arraylist ),
* Then, traverse the collection of the two transit sites to obtain the final transit information (the final transit information is also stored in arraylist)
*/
Package bustransfer;

Import java. SQL. connection;
Import java. SQL. drivermanager;
Import java. SQL. resultset;
Import java. SQL. resultsetmetadata;
Import java. SQL. sqlexception;
Import java. SQL. statement;
Import java. util. arraylist;
Import java. util. iterator;
Import java. util. List;
Import java. util. hashmap;
Import java. util. Map;

Public class bustransfer {

Private string start = NULL; // departure location

Private string whither = NULL; // target location

Private list schedule = NULL; // used to cache train schedules.

Private hashmap <string, arraylist> stationsofline = NULL; // all bus lines. Each list stores all stations that pass through the line.

Private hashmap <string, arraylist> linesofstation = NULL; // All stations. Each list stores all trains passing through the station.

// Private arraylist <string> Startline = new arraylist <string> ();//
// All trains that pass through the departure location.

// Private arraylist <string> whitherline = new arraylist <string> ();//
// All trains passing through the destination.

Private arraylist <map> firlinestalist = new arraylist <map> ();

Private arraylist <map> seclinestalist = new arraylist <map> ();

Public bustransfer (string start, string whither ){
This. Start = start;
This. Whither = whither;

Try {
This. Schedule = This
. Executequery ("select busline, up, stationno from bus_stations ");
} Catch (exception e ){
// Todo auto-generated Catch Block
System. Out. println ("database read error ");
E. printstacktrace ();
}
Stationsofline = This. getstationsofline ();
Linesofstation = This. getlinesofstation ();
}

Private hashmap <string, arraylist> getstationsofline (){
Hashmap map = new hashmap (); // used to temporarily store the hashmap retrieved from schedule.
Arraylist <map> line = NULL; // each list stores information about a vehicle trip.
String Buffer = "A"; // cache site name.
String temp = NULL; // temporary storage of the site name retrieved by each iteration, used for comparison with the buffer site name.
Hashmap <string, arraylist> stationsgroupbyline = new hashmap <string, arraylist> (); // stores the list of stations grouped by trains

Iterator it = schedule. iterator (); // iterator
While (it. hasnext ()){
Map = (hashmap) it. Next ();

Temp = (string) map. Get ("busline ");
If (stationsgroupbyline. containskey (temp )){
Line = stationsgroupbyline. Get (temp );
Buffer = (string) (MAP) line. Get (0). Get ("busline ");
}
If (buffer. Equals (temp )){
Line. Add (MAP); // put the station of the same train count into a list

} Else {
If (line! = NULL &&! Line. isempty ()){
Stationsgroupbyline. Put (buffer, line); // store the list composed of stations after the trains are grouped into a map
}
Line = new arraylist <map> (); // line references a newly constructed list to store trains at the same station.
Line. Add (MAP); // put the station of the same train entry into the empty list just constructed
}
Buffer = temp; // cache the current number of trains for the operation.
}
Return stationsgroupbyline;
}

Private hashmap getlinesofstation (){
Hashmap map = new hashmap (); // used to temporarily store the hashmap retrieved from schedule.
Arraylist <map> station = NULL; // each list stores information about the number of trains that pass through the station.
String Buffer = "A"; // cache the number of trains.
String temp = NULL; // temporary storage of the number of trains retrieved by each iteration, used for comparison with buffer trains.
Hashmap <string, arraylist> linesgroupbystation = new hashmap <string, arraylist> (); // stores the list of trains grouped by station

Iterator it = schedule. iterator (); // iterator
While (it. hasnext ()){
Map = (hashmap) it. Next ();

Temp = (string) map. Get ("up ");
If (linesgroupbystation. containskey (temp )){
// The station stores the site information corresponding to the temp train count.
Station = linesgroupbystation. Get (temp );
// Retrieve the linesgroupbystation information from the station, and cache and change the station name
// Compare with the station information stored in the just-retrieved Map
Buffer = (string) (MAP) station. Get (0). Get ("up ");
}
If (buffer. Equals (temp )){
// If the site information exists in the station, the site and the station are stored on the same site, so
// Put the stations of the same train count into a list
Station. Add (MAP );

} Else {
If (station! = NULL &&! Station. isempty ()){
// Store the list composed of stations after the trains are grouped into a map
Linesgroupbystation. Put (buffer, station );
}
// Line Re-references a newly constructed list for storing all trains passing through another station
Station = new arraylist <map> ();
Station. Add (MAP); // put the station of the same train number into the empty list just constructed
}
Buffer = temp; // cache the current number of trains for the operation.
}
Return linesgroupbystation;
}
/**
* Site filtering rules: add sites that comply with the Rules
*
* @ Param startsta
* @ Param whithersta
*/
Private void getstationsinline (string startsta, string whithersta ){
// Obtain all bus times that pass through the initial site
Arraylist firtrainline = linesofstation. Get (startsta );
// Obtain the number of bus passes through all destination sites
Arraylist sectrainline = linesofstation. Get (whithersta );

Arraylist station;
Hashmap line = NULL;
Int transferstano = 0;
// String stationname = NULL;
String trainno = "";

If (firtrainline! = NULL ){
Iterator firit = firtrainline. iterator ();
While (firit. hasnext ()){
// Retrieves a hashmap storing the station information
Line = (hashmap) firit. Next ();
// Retrieve the vehicle count Information
Trainno = (string) line. Get ("busline ");
Transferstano = (integer) line. Get ("stationno ");

// Retrieve the information of all sites that pass by the train trainno.
Station = stationsofline. Get (trainno );
Iterator it = station. iterator ();
While (it. hasnext ()){

Map map = (MAP) it. Next (); // trainno's map.
Int I = (integer) map. Get ("stationno ");
// Filter site rules: if the distance between the site and the initial site is greater than that of the initial site, the site is stored
// Firlinestalist, and vice versa, so those sites do not have to be added to firlinestalist.
If (I> transferstano ){
//
Firlinestalist. Add (MAP );
}
}
}
}

If (sectrainline! = NULL ){
Iterator secit = sectrainline. iterator ();
While (secit. hasnext ()){
Line = (hashmap) secit. Next ();
Trainno = (string) line. Get ("busline ");
Transferstano = (integer) line. Get ("stationno ");

Station = stationsofline. Get (trainno );
Iterator it = station. iterator ();
While (it. hasnext ()){

Map map = (MAP) it. Next ();
Int I = (integer) map. Get ("stationno ");

If (I <transferstano ){

Seclinestalist. Add (MAP );
}
}
}
}
}

/**
*
* Create Date: 2008-5-19 Author: Administrator
*
* @ Return
*/
Private arraylist <map> checkcrossline (){

Arraylist <map> crosslinelist = new arraylist <map> (); // a set of intersection lines, that is, a set of all transfer methods.
Arraylist <map> lsstart = firlinestalist; // The stop information of all trains that pass through the origin site.
Arraylist <map> lsend = seclinestalist; // The stop information of all trains that pass through the target station.

If (lsstart! = NULL &&! Lsstart. isempty () & lsend! = NULL
&&! Lsend. isempty ()){

For (Map <string, string> mapstart: lsstart ){
For (Map <string, string> mapend: lsend ){
If (isinthesamecity (mapstart. Get ("up"), mapend. Get ("up "))){

// Store the Intersection Line information into crossline to store a specific transfer method
Map <string, string> crossline = new hashmap <string, string> (
4, 0.8f );

// Set the first vehicle record as crossline
Crossline. Put ("firstline", mapstart. Get ("busline "));
// Place the number of trains to be transferred to crossline
Crossline. Put ("secondline", mapend. Get ("busline "));
// Put the transit site into crossline
Crossline. Put ("transfersta", mapend. Get ("up "));

// Crossline. Put ("transfersta", (string) startinf. Get ("up "));
// Store the hashmap containing the Intersection Line information to the list
// Put a specific transfer method into crosslinelist
Crosslinelist. Add (crossline );
} Else {
Continue;
}
}
}

} Else {
Crosslinelist = NULL;
}

Return crosslinelist;
}

Private Boolean isinthesamecity (string station1, string station2 ){

If (station1.contains (station2) | station2.contains (station1 )){
// System. Out. println (station1 + "#########" + station2 );
Return true;
} Else {
Return false;
}

}

Public arraylist <map> getschemaoftransfer (){
This. getstationsinline (this. Start, this. Whither );
Return this. checkcrossline ();
}

Public static void main (string [] ARGs ){
Bustransfer TB = new bustransfer ("Qianmen", "Tiananmen West ");
// TB. getschemaoftransfer ();
For (MAP map: Tb. getschemaoftransfer ()){
System. Out. println (MAP );
System. Out. println ("Hello, you can ride" + map. Get ("firstline") + ""
+ Map. Get ("transfersta") + "transfer" + map. Get ("secondline ")
+ "You will be able to arrive. Do not miss the website renewal! ");
}

// System. Out. println (Tb. seclinestalist. Size ());
}

Private connection getconnection (){
Connection con = NULL;
String url = "JDBC: mysql: // FIG: 3306/souwhat? Autoreconnect = true & useunicode = true & characterencoding = GBK & amp; mysqlencoding = GBK ";
String user = "root ";
String psword = "";
Try {
Class. forname ("com. MySQL. JDBC. Driver ");
} Catch (classnotfoundexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("the exception at load the driver ");
}
Try {
Con = drivermanager. getconnection (URL, user, psword );
} Catch (sqlexception e ){
// Todo auto-generated Catch Block
E. printstacktrace ();
System. Out. println ("the exception at creat the connection ");
}
Return con;
}

Private void closeconnection (connection conn) throws exception {
If (Conn! = NULL ){
Conn. Close ();
}
}

Private list executequery (string SQL) throws exception {
// System. Out. println ("executequery (SQL):" + SQL );
List list = new arraylist ();
Connection conn = NULL;
Statement stmt = NULL;
Resultset rs = NULL;
Try {
Conn = getconnection ();
Stmt = conn. createstatement ();

System. Out. println (SQL );

Rs = stmt.exe cutequery (SQL );

Resultsetmetadata rsmd = Rs. getmetadata ();
While (Rs. Next ()){
Map map = new hashmap ();
For (INT I = 1; I <= rsmd. getcolumncount (); I ++ ){
// All columns in each row are stored in hashmap.
Map. Put (rsmd. getcolumnname (I), RS. GetObject (I ));
}
// Store all rows in the list
List. Add (MAP );
}
} Catch (exception e ){
System. Out. println ("database query error! ");
E. printstacktrace ();
} Finally {
If (RS! = NULL)
Rs. Close ();
Closeconnection (conn );
}
Return list;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.