Definition:
A graph is a structure that is made up of vertices (also called nodes or points)
which are connected by edges (also called links or lines). A distinction is made between undirected graphs, where
edges link two vertices symmetrically, and directed graphs, where edges link two vertices
asymmetrically. see more on Graph Theory - Wikipedia
In this project we will illustrate to the best of our capabilities a Directed Weighted Graph and some familiar
Graphs Algorithms.
The graph resembles a small area with significant places and ways to transport between each place.
This can be used for a variety of purposes (best route between cities,routes etc.).
In our project we will try to simulate it as closely as can be to a small area as we said before.
In this project we were asked to do the following things:
Note: each one of the names here is a class/interface/test in our project and of the references is a link to the implementations.
-
Implement the following Interfaces:
-
Implement a class to each of the Interfaces:
-
Implement a test to each of the classes:
-
Build a Graph from a Json file:
- ParseToGraph17
-
Implement a GUI (graphics) that will show the Graph:
jump to GUI
jump to UML
jump to How to use instructions
jump to Sources
Our project structure:
- Node - Node represents a significant place in Graphs i.e. building,market etc. Each node holds information
regarding itself such as: street name, tag etc.
In this project each Node holds his 2d coordinates and an ID. - Location - GeoLocation represents the real life (latitude and longitude).
- Edge - Edge resembles a way of transport (road,sidewalk etc.) between two nodes.
Edges contain information as well for example road 4 ,etc. Not all edges are two-way edges. Because there are one way roads. In addition, the cost of each edge is not the same for similar reasons.
Graph is built upon founded on those. Graph contains all the nodes and all the edges in a given place, nodes can be
added and removed, same goes for edges.
Through it, we will represent a small area with places of significance and a way to modify them quickly providing
elasticity.
Algorithms are the main event over here.
As can be seen, this graph can represent real life places.
As so, we would want the best(shortest/chipset) way from a Node to another Node, this will be done by calculating the best way through each edges from each
node. This will be done with the Shortest path Algorithm.
In addition, we would like to know the time of each trip, as for we can retrieve the cost of the way from one node to
another. This will be done with The Shortest path dist Algorithm.
Moreover, we would like to know the best way between one node with a few stops on the way. This will be done through the
best effort TSP Algorithm.
At last, we would like to know the center of the place, which place is probably closest to all. This will be done through
Center Algorithm.
Those are the main Algorithms we will use to represent as closely as possible real life situations.
Finally, we would like a visualisation way to show the users the graph, edges, the best roads etc.
As a result we created user interface which will visualize the graph and algorithms mentioned above.
It will display the nodes and edges between all the nodes. Algorithms will be easily activated to illustrate the
situations.
This will be a user-friendly gui which aspires to be easy to use, adjustable and illustrate our project as good as we
can.
In conclusion, you will be able to see the way and understand much better what is the best result for your request.
Here are the results of the algorithm on a connected graph.
The left column has the function we activated. The first row has the number of nodes.
1000/20000 | 10000/200000 | 100000/2000000 | 1000000/200000000 | |
---|---|---|---|---|
is Connected | 0.01s | 0.2s | 5-17s | NULL |
TSP 10 Nodes | 0.02s | 0.5-1s | 1-10s | NULL |
Center | 2s | 5.5m | NULL | NULL |
Shortest Path | 0.02s | 0.04s | 5s | NULL |
We are able to generate a graph with 1mk nodes and 20mk edges but are unable to run any algorithm on it, due to heap space. The results vary depends on how th graph is generated.
Since we did not create the graph randomly most of the time, it didn't take so much time.
Implementing the GUI was a major part (and not easy at all) of this project because we wanted to give the user the Max comfort and simplicity that we could think of.
the GUI contains 4 classes:
-
MyFrame - MyFrame is the main part of this section, basically it holds all the components of the programs and creating them.
-
MyPanel - MyPanel is where the magic happen, MyPanel is the main panel that holds all the graph structure and responsible for drawing it correctly.
-
Help - Help is a small class that creates only a new JFrame to the main frame. The purpose of Help is to open a new window that contains all the shortcuts available.
-
MyGraph_GUI - MyGraph_GUI creates the main JFrame and then activating the GUI.
Here we attached a simple image of the GUI.
The frame of the GUI has 4 main parts:
- Menu Bar
- Buttons Panel
- The Graph
- Action Log
-
Menu Bar/Buttons Panel - From them the user can execute all kind of features.
Not all the features in the Menu Bar appears in the Buttons Panel, but all the features in Buttons Panel appears in the Menu Bar.Features list:
Note: the obvious features doesn't have an explanation/examples because their pretty simple.- Algorithms:
- isConnected // Algorithms Bar and Buttons Panel, jump to isConnected
- TSP // Algorithms Bar and Buttons Panel, jump to TSP
- Center // Algorithms Bar and Buttons Panel, jump to Center
- ShortestPath // Algorithms Bar and Buttons Panel, jump to ShortestPath
- Edit Graph:
- Remove Node // Edit Bar and Buttons Panel, jump to Remove Node
- Remove Edge // Edit Bar and Buttons Panel, jump to Remove Edge
- Add Node // Edit Bar and Buttons Panel, jump to Add Node
- Add Edge // Edit Bar and Buttons Panel, jump to Add Edge
- View settings:
- Hide/Show Buttons // View bar only
- Full Screen // View bar only
- Default Screen // View bar only
- Costume Screen // View bar only
- Help:
- Shortcuts // Help bar only, jump to Shortcuts
- File:
- Load // File Bar and Buttons Panel
- Save // File Bar and Buttons Panel
- Clear // File Bar and Buttons Panel
- Reset Graph // File Bar and Buttons Panel
- Exit // File bar only
- Algorithms:
-
The Graph - The Graph holds all the relevant data in order to keep updating the drawings.
-
Action Log - The Action Log purpose is helping the user to control and monitor all the changes he did with the graph. Every Button click or action made in the program the Action Log writes it down, for example: if the user clicked on the Center button, so after he got an update through a popup message it's also written in the Action Log.
After isConnected is pressed, a new popup window appears with the answer to the question:
is the Graph strongly connected?
jump to GUI-Tutorial
In the TSP we decided to give the user two ways to type the input:
- One long String.
- Each time one String.
- This option allows the user to type all the input in one time without the need to type each time one Node.
- The String format is:
"5 3 9 12 15 7 0",
each Node is seperated with a single space character.
- This option will create a new popup window that asks the user to enter another Node, this process will finish once the
user type "done"/"DONE".
- The path will be colored in light purple on the Graph.
- At the action log the user can see:
- List of the Nodes he entered.
- List of the path in the right order that go through every one of the Nodes he entered.
- In case of any Invalid input the user will receive a popup window mentioning the problem, and it will be written
in the action log too.
jump to GUI-Tutorial
- List of the Nodes he entered.
Once the user pressed the Center 3 things will happen:
1. Popup window will appear with the ID of the Node that our Algorithm chose to be the Center.
2. The Node with this ID will change his color from blue to red, and also the white box where his ID written
will change to yellow.
3. The action log will write also this Node as the Center.
jump to GUI-Tutorial
Once the user pressed the ShortestPath 3 things will happen:
- Popup window will open and ask the user to enter source Node and destination Node
- The ShortestPath that our Algorithm chose will be marked in green.
- The action log will write 2 things:
- Weight of the ShortestPath.
- String represents the ShortestPath in the right order.
In case the user entered an invalid input, he will get a popup window mentioning what is the problem, and this will also be written in the log.
There are 2 types of wrong inputs:
- No input at all or String of chars, something that is not an Integer.
- source/destination/both are not in the Graph.
Remove Node is simple, give me ID, and I'll delete the Node.
After the user types the key the program checks if this Node is in the Graph, if true it deletes the Node, else meaning
wrong input.
Wrong values/inputs are:
- No input or a String.
- ID that is not in the Graph.
Output:
If the input is valid the user will see that the Node he picked was removed from the Graph and all it's Edges
too.
Otherwise, a popup window will appear with the cause written, and it will be added to the action log.
After the Node was deleted, the Action log will write the Node number that was removed.
jump to GUI-Tutorial
Remove Edge is similar to Remove Node only with 2 inputs.
After the user types the source and the destination the program checks:
- if the source Node and the destination Node are Nodes in the Graph.
- if there is an Edge between source to destination.
if both of them are true the Edge will be deleted from the Graph.
Wrong values/inputs are:
- No input or a String.
- source/destination that is not in the Graph or not connected.
If the input is valid the user will see that the Edge he picked was removed from the Graph. Otherwise, a popup window will appear with the cause written, and it will be added to the action log. After the Edge was deleted, the Action log will write the details of the Edge that was deleted.
jump to GUI-Tutorial
In order to add a new Node to the Graph the user need to type x,y,id.
- The (x,y) representing the location of the point on the Graph.
- The id represents an ID, each Node has an uniq ID.
valid inputs are:
- x,y are numbers so any input that is not a pure number will cause an Invalid Input message.
- id is a number too, and it must be an uniq id.
Output:
If the input was valid the user will see the new Node immediately on the Frame.
In addition, the Action Log that will write the details of the new Node.
jump to GUI-Tutorial
In order to add a new Edge to the Graph the user need to type source and destination.
- The source is the Node where the new Edge will begin.
- The destination is the Node where the new Edge will end.
valid inputs are: - The source and the destination must be a Nodes that are existed in the Graph.
- The source and destination must be a numeric values.
If the input was valid the user will see the new Edge immediately on the Frame.
In addition, the Action Log that will write the details of the new Edge.
jump to GUI-Tutorial
The shortcuts' purpose is one more way that we tried to make it easier on the user.
By clicking the Shortcuts in the Help bar a new Frame will appear with a description of all the shortcuts available
in this program.
By clicking BACK the user will return to the main Frame.
Let's begin with cloning the repository.
git clone https://github.com/netanellevine/Weighted_Graph_Algorithms.git && cd Weighted_Graph_Algorithms
Then we would like to run the jar to view the graph.
For that we have few options:
1. type/copy the blockquotes below as is.
- First, it loads an empty graph and will open an empty frame with all the GUI options.
- Second, now create a graph you desire or load one.
java -jar weighted-graphs.jar
2. type/copy the blockquotes below and add it to the end the line random.
- Load a random generated graph (that we created already).
- The graph is loaded, play with all the features.
java -jar weighted-graphs.jar random
3. type/copy the blockquotes below and add an existing json_file in order to parse it into a graph.
- Load a graph with json file as a String.
- The graph is loaded, play with all the features.
java -jar weighted-graphs.jar <json-file>
In the data directory we have a few examples of some graph representation in json format. for example: " \Weighted_Graph_Algorithms\data\G1.json"
- Djikstra's algorithm using a priority queue - Youtube
- Check if a graph is strongly connected
back to top