js newbie "A" wants to learn React from "B" and wants to know in his newtwork who can introduce him to B in the shortest time period.
Input format:
Total Members in UI friend Network = N
MemberId1 = N1
MemberId2 = N2
MemberId3 = N3
MemberIdN = Nn
Output format:
shortest time A takes to reach B
Sample Input:
4
2
5
7
9
4
2 9 2
7 2 3
7 9 7
9 5 1
7
9
Sample Output:
5
Solution:3
public class Graph {
static class Node {
private String name;
private List<Node> shortestPath = new LinkedList<>();
private Integer distance = Integer.MAX_VALUE;
Map<Node, Integer> adjacentNodes = new HashMap<>();
public void addDestination(Node destination, int distance) {
adjacentNodes.put(destination, distance);
}
public Node(String name) {
this.name = name;
}
// getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Node> getShortestPath() {
return shortestPath;
}
public void setShortestPath(List<Node> shortestPath) {
this.shortestPath = shortestPath;
}
public Integer getDistance() {
return distance;
}
public void setDistance(Integer distance) {
this.distance = distance;
}
public Map<Node, Integer> getAdjacentNodes() {
return adjacentNodes;
}
public void setAdjacentNodes(Map<Node, Integer> adjacentNodes) {
this.adjacentNodes = adjacentNodes;
}
}
private Set<Node> nodes = new HashSet<>();
public void addNode(Node nodeA) {
nodes.add(nodeA);
}
// getters and setters
public static Graph calculateShortestPathFromSource(Graph graph, Node source) {
source.setDistance(0);
Set<Node> settledNodes = new HashSet<>();
Set<Node> unsettledNodes = new HashSet<>();
unsettledNodes.add(source);
while (unsettledNodes.size() != 0) {
Node currentNode = getLowestDistanceNode(unsettledNodes);
unsettledNodes.remove(currentNode);
for (Map.Entry<Node, Integer> adjacencyPair :
currentNode.getAdjacentNodes().entrySet()) {
Node adjacentNode = adjacencyPair.getKey();
Integer edgeWeight = adjacencyPair.getValue();
if (!settledNodes.contains(adjacentNode)) {
calculateMinimumDistance(adjacentNode, edgeWeight, currentNode);
unsettledNodes.add(adjacentNode);
}
}
settledNodes.add(currentNode);
}
return graph;
}
private static void calculateMinimumDistance(Node evaluationNode,
Integer edgeWeigh, Node sourceNode) {
Integer sourceDistance = sourceNode.getDistance();
if (sourceDistance + edgeWeigh < evaluationNode.getDistance()) {
evaluationNode.setDistance(sourceDistance + edgeWeigh);
LinkedList<Node> shortestPath = new LinkedList<>(sourceNode.getShortestPath());
shortestPath.add(sourceNode);
evaluationNode.setShortestPath(shortestPath);
}
}
private static Node getLowestDistanceNode(Set<Node> unsettledNodes) {
Node lowestDistanceNode = null;
int lowestDistance = Integer.MAX_VALUE;
for (Node node : unsettledNodes) {
int nodeDistance = node.getDistance();
if (nodeDistance < lowestDistance) {
lowestDistance = nodeDistance;
lowestDistanceNode = node;
}
}
return lowestDistanceNode;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
List<Node> nodeList = new ArrayList<>();
for (int i = 0; i < n; i++) {
st = new StringTokenizer(br.readLine());
nodeList.add(new Node(st.nextToken()));
}
int e = Integer.parseInt(br.readLine());
for (int i = 0; i < e; i++) {
st= new StringTokenizer(br.readLine());
Node SourceNode = searchNode(nodeList,st.nextToken());
Node destination =searchNode(nodeList,st.nextToken());
SourceNode.addDestination(destination, Integer.parseInt(st.nextToken()));
}
Node source = null;
st= new StringTokenizer(br.readLine());
String src=st.nextToken();
st=new StringTokenizer(br.readLine());
String dst=st.nextToken();
Graph graph = new Graph();
for (Node node:nodeList){
graph.addNode(node);
}
for(Node node :graph.nodes){
if (node.name.equalsIgnoreCase(src)){
source=node;
break;
}
}
graph = calculateShortestPathFromSource(graph, source);
for(Node node: graph.nodes){
if (node.name.equalsIgnoreCase(dst)){
System.out.println(node.distance);
}
}
}
private static Node searchNode(List<Node> nodeList, String nextToken) {
for (Node node: nodeList ){
if (node.name.equalsIgnoreCase(nextToken))
return node;
}
return null;
}
}
Comments
Post a Comment