-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph_Utils.java
More file actions
55 lines (47 loc) · 1.22 KB
/
Graph_Utils.java
File metadata and controls
55 lines (47 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.dsj.graphs;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Graph_Utils {
Scanner sc;
Map<Integer, String> arrIndexToVertexMap;
int numberOfVertices;
public Graph_Utils() {
getNumberOfVertices();
}
void getNumberOfVertices() {
sc = new Scanner(System.in);
System.out.println("Enter the total number of vertices.");
numberOfVertices = sc.nextInt();
}
/**
* Get the name of the vertices. Every thing that is entered is assumed
* taken-in as a string.
*/
void getVertices() {
System.out.println("Enter your vertices:");
arrIndexToVertexMap = new HashMap<>();
try {
for (int i = 0; i < numberOfVertices; i++) {
arrIndexToVertexMap.put(i, sc.next());
}
sc.close();
} catch (RuntimeException re) {
System.out.println("Error at method:: getVertices || Description:" + re);
}
}
/**
* @param vertex
* Name of the vertex
* @return The unique index for this string from the hash-map.
*/
int getIndexForThis(String vertex) {
int i = 0;
for (; i < numberOfVertices; i++) {
if (arrIndexToVertexMap.get(i).equalsIgnoreCase(vertex)) {
break;
}
}
return i;
}
}