Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/skpro-glitch/circuitswitching
Simulation of Circuit Switching in Java to find optimal set of paths for multiple data transfers using Dynamic Programming. - Soham Kapur, VIT Chennai
https://github.com/skpro-glitch/circuitswitching
circuit-switching computer-network computer-network-basics computer-networking computer-networking-and-internet-technologies computer-networking-lab computer-networks computer-networks-course computer-networks-lab computer-networks-project computer-science
Last synced: about 3 hours ago
JSON representation
Simulation of Circuit Switching in Java to find optimal set of paths for multiple data transfers using Dynamic Programming. - Soham Kapur, VIT Chennai
- Host: GitHub
- URL: https://github.com/skpro-glitch/circuitswitching
- Owner: SKpro-glitch
- Created: 2023-08-20T05:24:03.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-07-14T15:00:23.000Z (4 months ago)
- Last Synced: 2024-07-14T16:49:52.496Z (4 months ago)
- Topics: circuit-switching, computer-network, computer-network-basics, computer-networking, computer-networking-and-internet-technologies, computer-networking-lab, computer-networks, computer-networks-course, computer-networks-lab, computer-networks-project, computer-science
- Language: Java
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CircuitSwitching
/** PROJECT DETAILS
* Authors : Soham Kapur, Koushiki Sinha
* Year : 2023
* Title : Circuit Switching using Dynamic Programming
* Purpose : Undergraduate Graded Project
* Course : Computer Networks
* Desc : Simulation of Circuit Switching in Java to find optimal set of paths for multiple data transfers using Dynamic Programming.
*//* VARIABLES:
* n: Number of data transfers
* k: Counter for current Source and Destination pair being checked
* s: Array to store all Souce nodes.
* d: Array to store all Destination nodes.
* min: Stores the minimum Longest path distance of the checked combinations ofpaths.
*///CODE BEGINS HERE:
import java.util.*;
public class CircuitSwitching
{
ArrayList[] adjList;
static int n=0, k=0, s[], d[], min=Integer.MAX_VALUE;
int Graph[][] = { { 0, 4, 0, 0, 0, 0, 0, 8, 0 },
{ 4, 0, 8, 0, 0, 0, 0, 11, 0 },
{ 0, 8, 0, 7, 0, 4, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 4, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 11, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 } };
/*
* This is the Graph (Network) consisting of nodes and edges expressed as an Adjacency Matrix, using a DDA.
* The index value represents the nodes and the array elements at the coordinates represent the Edge weight between the 2 nodes.
* The edge with direction from node u to node v will be at Graph[u][v].
* The sample graph used here is an Undirected Graph, but a Directed Graph can also be used without any other change in the code.
*/
//This copy of the graph is created so that the user can use this code in a loop (not present in this code) without changing the original Graph.
int altGraph[][] = Graph;
//The mpdifications to the original Graph present in the Alternate Graph (altGraph) will be explained later in the code.int v = Graph[0].length;
boolean visited[] = new boolean[v];
static ArrayList>[] allPaths;
//Data structure to store all possible paths between 2 nodes
static ArrayList[] distances;
//Data structure to store total distance or weight of each path between 2 given nodes
static ArrayList[] FinalPaths;
//Data Structure to store the final set of paths that should be chosen// Constructor to initialise 'v', the number of nodes
public CircuitSwitching()
{
this.v = v;
initAdjList();
}// utility method to initialise adjacency lists
void initAdjList()
{
int i;
adjList = new ArrayList[v];
for (i = 0; i < v; i++)
adjList[i] = new ArrayList<>();allPaths = new ArrayList[n];
for(i=0; i();distances = new ArrayList[n];
FinalPaths = new ArrayList[n];
for(i=0; i();
}/* The graph is transferred from a DDA to an Array of ArrayLists.
* This prevents checking of pairs of nodes that do not have any edge, thus improving speed in the following operations.
*/
public void addEdges()
{
int n=v;
for(int i=0; i0)
adjList[i].add(j);
}/* In the alternate graph, all incoming edges of every Source node and all outgoing edges of every Destination node are eliminated.
* This reduces the number of edges that the program has to check, thus improving speed and efficiency.
*/
public void AltGraph()
{
for(int i=0; i pathList = new ArrayList<>();
// add source to path[]
pathList.add(s);
// Call utility function
getAllPathsUtil(s, d, isVisited, pathList, 0);
}// Parts of this function have been taken from the code for finding All Paths in a Graph, available on "Geeks For Geeks"
private void getAllPathsUtil(Integer u, Integer d, boolean[] isVisited, ArrayList localPathList, int distance)
{
/* An if condition that prevents the checking of some extra paths by terminating the recursion if the destination node is adjacent to the current node.
* This prevents the checking of some useless paths and saves time.
* It assumes that the direct edge between any two nodes is the shortest possible path between them.
* This condition is only useful when all edges have positive weights.
*/
if(altGraph[u][d]>0)
{
localPathList.add(d);
allPaths[k].add(new ArrayList<>(localPathList));
distances[k].add(distance + altGraph[u][d]);
localPathList.remove(d);
return;
}
isVisited[u] = true;
// This loop having recursion checks the possibility of creating a path between the Source and Destination using every node.
for (Integer i : adjList[u])
{
if (!isVisited[i])
{
// store current node in path
localPathList.add(i);
distance += altGraph[u][i];
getAllPathsUtil(i, d, isVisited, localPathList, distance);
distance -= altGraph[u][i];
// remove current node in path
localPathList.remove(i);
}
}
isVisited[u] = false;
}//This function uses Bubble Sort to sort the paths between every pair of Source and Destination nodes with respect to total distance/weight.
void sortPaths()
{
for(int h=0; h distances[h].get(j+1))
{
Collections.swap(allPaths[h], j, j+1);
Collections.swap(distances[h], j, j+1);
}
}
}//This function prints all the paths between every pair of Source and Destination nodes and their respective total distances.
public void printPaths()
{
for(int i=0; i[] finalPath)
{
if(p==n)
{
// This is the if statement that finalises the combination based on the set condition.
if(min>max)
{
System.out.println();
min = max;
finalise(finalPath);
System.out.println(finalPath);
}
return;
}
int i, j, l = allPaths[p].size();
//This loop recursively creates all combinations of paths to ultimately check the optimality condition.
for(i=0; i();
finalPath[p] = allPaths[p].get(i);
AltPaths(p+1, max, finalPath);
unvisit(finalPath[p], p);
finalPath[p].clear();
}
}
//This function checks if any node in the selected path is already present in a previously selected path.
boolean checkIntersect(ArrayList path)
{
for(int i=1; i path, int p)
{
for(int i=1; i path, int p)
{
for(int i=1; i[] finalPath)
{
for(int i=0; i(finalPath[i]);
}// This funciton prints the finalised combination, and if there is no data then prints the appropriate message.
void printFinal()
{
System.out.println("Max distance: " + min + "\nFinal list of paths is: ");
for(int i=0; i[] finalPath = new ArrayList[n];
int i, j;
for(i=0; i();CircuitSwitching g = new CircuitSwitching();
for(i=0; i();
g.getAllPaths(s[i], d[i]);
}
System.out.println("\n\nSorted Paths: ");
g.sortPaths();
g.printPaths();
System.out.println("\n");
g.AltPaths(0, 0, finalPath);
g.printFinal();
}
}