Giter VIP home page Giter VIP logo

internetprogramming-graphalgorithms's Introduction

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)

internetprogramming-graphalgorithms's People

Contributors

edenzadikove avatar haimadrian avatar orelger avatar

Watchers

 avatar

Forkers

edenzadikove

internetprogramming-graphalgorithms's Issues

2. Implement unit tests for "Shortest Paths between u and v" algorithm

Write some unit tests that test the algorithm implemented at #3 , with various inputs, to make sure the output of the algorithm is correct.
Use at least:
Graph with no path
Graph with a single path
Graph with 3 shortest paths
Graph with one shortest path, but having more than 1 path

Make sure the paths might contain circles, and that they are ignored.

1. Implement unit tests for "Connected Components" algorithm

Write some unit tests that test the algorithm implemented at #1 , with various inputs, to make sure the output of the algorithm is correct.
Use at least:
Graph with no 1's
Graph with a single connected component
Graph with 3 connected components

Make sure the connected components are not simple.
For example: (Two connected components)
[ 1, 1, 0, 1, 1 ]
[ 1, 0, 0, 1, 1 ]
[ 1, 1, 0, 1, 1 ]
[ 1, 0, 0, 1, 1 ]

4. Implement "Shortest paths in a weighted graph" algorithm

Algorithm 4
Implement an algorithm such that given a graph and two indices (source and destination) the algorithm will find all shortest paths from source index to second index.
In this algorithm, we use a weighted graph, so the algorithm should find the paths with the minimum total weight.

(Dijkstra and BFS are not working here. Find Dijkstra that works with negative numbers: Bellman-Ford Algorithm)

3. Implement unit tests for "Submarines" game

Write some unit tests that test the game implemented at #5 , with various inputs, to make sure the output of the algorithm is correct.
Use at least:
Graph with no submarines
Graph with a single submarine
Graph with illegal submarine (length = 1)
Graph with illegal submarine (cross)
Graph with 3 submarines, according to Nathan's example

Make sure the submarines are not simple. (use some with width and height bigger than 1.)
For example:
[ 1, 1, 0, 1, 1 ]
[ 1, 1, 0, 1, 1 ]
[ 0, 0, 0, 1, 1 ]
[ 1, 1, 0, 1, 1 ]

3. Implement "Submarines" game

Algorithm 3

  • A submarine must be of length 2 at least. (vertical or horizontal, but not cross)
  • Minimum distance between two submarines is 1 cell. e.g. [ 1, 1, 0, 1, 1]
  • A submarine might be wider than 1 cell. e.g.
    [ 1, 1, 0 ]
    [ 1, 1, 0 ]

Output of the game is the amount of legal submarines on a board. (matrix/graph)

2. Implement "Shortest Paths between u and v" algorithm

Algorithm 2
Implement an algorithm such that given a graph and two indices (source and destination) the algorithm will find all shortest paths from source index to seconds index.
This task is limited to use a single thread only.

1. Implement "Connected Components" algorithm in a graph

Algorithm 1
In this algorithm we need to find all connected components (standard and cross) in a graph, and return them.
Check if multiple threads can be used in order to make the search operation faster. For example, assume a 500x500 matrix, multiple threads can scan it faster. Though for a 10x10 matrix, spawning threads might be redundant overhead.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.