Skip to content

Repository for Internet Programming course - Implementing parallel graph algorithms, such as BFS, DFS, Dijkstra and Bellman-Ford

License

Notifications You must be signed in to change notification settings

haimadrian/InternetProgramming-GraphAlgorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Internet Programming Project

Repository to work on the final project in Internet Programming course, HIT, third year, 2nd semester

Note

You'll need Gradle and Lombok in order to open, and successfully run, the modules below
The 3 of the modules can be opened and configured quickly if you open the project itself, which contains the 3 modules. For this, open build.gradle As Project.

Server

Source
Server is implemented with java, listening on port 8005 with TCP server (ServerSocket).
The TCPServer class is a common TCP server implementation, supports custom handler to re-use this implementation in other projects.
TCPServer forwards the accepted operational sockets to ClientHandler which reads requests from socket's input stream (using the custom handler), and forwards the requests to the custom handler mentioned above.
Our custom handler is: MatrixClientHandler which supports clear JSON body or clear HTTP GET requests. We support basic HTTP to make algorithms reachable through a Web Browser. When the request content is a clear jJSON, we handle it by unmarshalling the request into Request model, and handling it using a Command Pattern implemented at ActionExecutor. Each action knows how to handle its request, based on command type, and return a generic Response.
In case of HTTP response, we wrap the response content with our index.html and send it back to the HTTP caller.
In case of regular request (Java client) we marshall the Response model into JSON, and write it back to the operational socket's output stream.
Scroll down to see class diagrams.
The server will create a tray icon when it runs in a Desktop environment, which lets us to ordinary shutdown the server. Scroll down to see screenshots.

Algorithms

The actions mentioned above (from command pattern) use their corresponding algorithms in order to perform user request, concurrently, and return the response.

  • DFSVisit: Used for finding connected component in a graph, starting from its root. The class uses ThreadLocal so we can find all connected components using multiple threads.
  • BFSVisit: Used for finding shortest paths between vertex s to vertex t. The class uses ThreadLocal, though we use a single thread to access it.
  • DijkstraWithNegCycleSupport: This is an improved version (though not that efficient) of dijkstra algorithm, used to find shortest paths in a weighted graph, including negative weights. When there is a negative cycle, we detect it and print a warning to log. Though this will not break the traverse operation, we'll continue looking for a shortest simple path. In this class we use ForkJoinPool with RecursiveAction, to make the traverse searching in parallel.
  • ConnectedComponents: Used to find all connected components in a graph, concurrently. This algorithm uses DFSVisit.
  • Submarines: Used to count how many submarines there are in a graph, concurrently. This algorithm uses the ConnectedComponents algorithm, in order to collect them and then check each CC and see if it is a submarine or not.
  • BellmanFord: Used to find shortest paths in weighted graph. This algorithm was replaced by Dijkstra since we couldn't make it run in parallel.. It runs using a single thread.

Thread Pools

There are four thread pools in the server.

  • Single thread executor, used by TCPServer to launch the server and let the main thread continue its execution.
  • Max-10-threads thread pool, used by TCPServer, to serve client requests concurrently
  • 4*availableProcessors-threads thread pool, used by ActionThreadService, to serve algorithms that use parallel-search. (e.g. ConnectedComponents, Submarines). It is shared to all instances in the server, to avoid of flooding the server with threads.
  • 1*availableProcessors-threads ForkJoin thread pool, used by ActionThreadService, to server Dijkstra algorithm, which is implemented using RecursiveAction, to run its search concurrently in Fork-Join mechanism. It is shared to all instances in the server, to avoid of flooding the server with threads.

Common

Source
Common project contains the models, as they are relevant for both client and server.
Scroll down to see class diagrams.

  • Index: A model that have row and column, used to access cells in an IMatrix. (This is also our vertex type in an IGraph)
  • IMatrix: A generic matrix for generic type T. In our project, the type is Integer which is used by binary matrices or weighted matrices.
  • IStandardMatrix: A matrix that supports going in UP, DOWN, LEFT, RIGHT directions.
  • ICrossMatrix: A matrix that supports going in UP-LEFT, UP-RIGHT, DOWN-LEFT, DOWN-RIGHT directions.
    The reason we use interfaces for Standard and Cross is to implement "multiple inheritance" in Matrix class, which supports going in ALL directions. (We re-use the default implementation of the interfaces mentioned above)
  • IGraph: A traversable model, having root, indices and edges.
  • MatrixGraphAdapter: Adapts the IGraph API, where the generic type of IGraph is set to Index, for an instance of IMatrix.

Notes

  • We use Jackson for marshalling/unmarshalling requests and responses, thus avoiding of unsafe usage of ObjectInputStream and ObjectOutputStream
  • We maintain a cache (string-heap-like) for instances of Index class, to avoid of too many creations of Index, and also make it possible to compare indices by using ==, and not equals. The cache is a weak cache, which means that the references can be garbage collected once there is no strong reference left referring to an index.
  • We use log4j2 to write messages to console / log file, having details about the server while it is running in background, and gain the ability to print messages in DEBUG level when necessary.

Client

Source
The client has a standard menu, using console, to guide its user about input/output.
We support saving graphs to file, so it will be easier to load graphs next time a client runs. The graphs are stored into json files at the client machine.

Class Diagrams

Generated using Intellij. Powered by yFiles.

Matrix and Graph

Actions (Command Pattern)

Algorithms

Flow of request handling in server

Screenshots

Tray Icon

Client Menu

Generate Graph

Connected Components

Count Submarines

Shortest Paths (BFS)

Shortest Paths (Dijkstra, standard matrix)

Shortest Paths (Dijkstra, regular matrix)

Home (Web Browser)

Generate Graph (Web Browser)

Connected Components, Regular Matrix (Web Browser, with statistics)

Connected Components, Standard Matrix (Web Browser, with statistics)

About

Repository for Internet Programming course - Implementing parallel graph algorithms, such as BFS, DFS, Dijkstra and Bellman-Ford

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published