Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions src/FlightRoutesGraph.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.HashSet;
import edu.greenriver.sdev333.MathSet;

public class FlightRoutesGraph {
//have to have two sets to model a graph (network)
// 1. a set of vertices (aka points or nodes) - in this case its our airports
// 2. a set of edges (aka connections, lines, relationships) - in this case its routes between airports

//helper class for our edges/connections
//a string from node1 to node2
private class Edge {
private String node1;
private String node2;

//helper constructor to build an edge
//a connection from one node to another node
//take in from and save it into node1
//take in to and save it into node2
public Edge(String from, String to) {
node1 = from;
node2 = to;
}
}

//our sets
private MathSet<String> nodes; //our nodes are string data types ex: JFK
private MathSet<Edge> edges; // our edges are connections between two nodes

//constructor for FlightRoutesGraph
public FlightRoutesGraph() {
nodes = new BSTSet<>(); //we can use BSTSet because our nodes are comparable
edges = new HashSet<>(); // our edges cannot be compared so we must put it in a HashSet
}

/**
* adding a city to the graph
* @param city
*/
public void addNode(String city) {
// when we get the city we put it into the nodes set
nodes.add(city);
}

/**
* adding a connection between 2 cities
* @param city1
* @param city2
*/
public void addEdge(String city1, String city2) {
//add the cities to the edge set
Edge connection = new Edge(city1, city2);
edges.add(connection);
}

// if someone gives me a city, we want to know its neighbors
// therefor the return type should be a set of strings
MathSet<String> getNeighbors(String city) {
// create an empty set to hold the results
MathSet<String> neighbors = new BSTSet<>();

//need to walk through the edges so we need to write a loop
// and check if the city is in either node1 or node2
for(Edge e : edges.keys()) {
if (e.node1.equals(city)) {
neighbors.add(e.node2);
}
else if (e.node2.equals(city)) {
neighbors.add(e.node1);
}
}

return neighbors;

}



//testing
public static void main(String[] args) {
FlightRoutesGraph g = new FlightRoutesGraph();

// add all the cities first
g.addNode("JFK");
g.addNode("ORD");
g.addNode("ATL");
g.addNode("MCO");
g.addNode("DEN");

// add connections between cities (edges, routes)
g.addEdge("JFK", "MCO");
g.addEdge("ATL", "MCO");
g.addEdge("DEN", "ORD");
g.addEdge("ORD", "ATL");

//look for direct flights from JFK
MathSet<String> directJFK = g.getNeighbors("JFK");
MathSet<String> directATL = g.getNeighbors("ATL");


}

}
59 changes: 59 additions & 0 deletions src/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
import edu.greenriver.sdev333.BSTSet;
import edu.greenriver.sdev333.HashSet;
import edu.greenriver.sdev333.MathSet;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");

//creating the string to test out
String testString = "T H I S S E T A B C";
String newString = "O T H E R S E T D E F";

// two sets that we are testing
MathSet<String> thisSet = new HashSet<>();
MathSet<String> otherSet = new HashSet<>();

//creating scanners to add the strings created above to the sets created
Scanner input = new Scanner(testString);
Scanner otherInput = new Scanner(newString);

//adding the strings as keys to thisSet
while (input.hasNext()) {
String key = input.next();
thisSet.add(key);
}

//printing out the first set
System.out.println("Set: " + thisSet);

//repeating the process for otherSet
while (otherInput.hasNext()) {
String key = otherInput.next();
otherSet.add(key);
}
System.out.println("other set: " + otherSet);

//Testing all the different methods ------------------

//if key is contained, true will be printed out
System.out.println(thisSet.contains("S"));
System.out.println(otherSet.contains("O"));
// the size of the set will be printed, an int will be returned
System.out.println(thisSet.size());
System.out.println(otherSet.size());
//the set has keys in it therefor it is not empty to false should be returned
System.out.println(thisSet.isEmpty());
System.out.println(otherSet.isEmpty());
// adding extra keys to our sets
thisSet.add("Y");
otherSet.add("Z");
System.out.println("this: " + thisSet);
System.out.println("other: " + otherSet);


//Testing the unique methods in a set -----------------------
System.out.println("intersection " + thisSet.intersection(otherSet));
System.out.println("union " + thisSet.union(otherSet));
System.out.println("difference " + thisSet.difference(otherSet));


}
}
Loading