summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/graph/AdjacencyMap.java
blob: 3b6d6efd0999018040a9adfbaff29c6760473575 (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package bjc.utils.graph;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.IntStream;

import bjc.utils.data.GenHolder;

/**
 * An adjacency map representing a graph
 * 
 * @author ben
 *
 * @param <T>
 *            The type of the nodes in the graph
 */
public class AdjacencyMap<T> {
	/**
	 * Create an adjacency map from a stream of text
	 * 
	 * @param stream
	 *            The stream of text to read in
	 * @return An adjacency map defined by the text
	 */
	public static AdjacencyMap<Integer> fromStream(InputStream stream) {
		Scanner scn = new Scanner(stream);
		scn.useDelimiter("\n");

		// First, read in number of vertices
		int numVertices = Integer.parseInt(scn.next());

		Set<Integer> vertices = new HashSet<>();
		IntStream.range(0, numVertices).forEach(e -> vertices.add(e));

		// Create the adjacency map
		AdjacencyMap<Integer> aMap = new AdjacencyMap<>(vertices);

		GenHolder<Integer> row = new GenHolder<>(0);

		scn.forEachRemaining((strang) -> {
			String[] parts = strang.split(" ");
			int col = 0;

			for (String part : parts) {
				aMap.setWeight(row.held, col, Integer.parseInt(part));

				col++;
			}

			row.held++;
		});

		scn.close();

		return aMap;
	}

	/**
	 * The backing storage of the map
	 */
	private Map<T, Map<T, Integer>> adjMap;

	/**
	 * Create a new map from a set of vertices
	 * 
	 * @param vertices
	 *            The set of vertices to create a map from
	 */
	public AdjacencyMap(Set<T> vertices) {
		vertices.forEach(src -> {
			Map<T, Integer> srcRow = new HashMap<>();

			vertices.forEach(tgt -> srcRow.put(tgt, 0));

			adjMap.put(src, srcRow);
		});
	}

	/**
	 * Check if the graph is directed
	 * 
	 * @return Whether or not the graph is directed
	 */
	public boolean isDirected() {
		GenHolder<Boolean> res = new GenHolder<>(true);

		adjMap.entrySet()
				.forEach(src -> src.getValue().entrySet().forEach(tgt -> {
					int lhs = tgt.getValue();
					int rhs = adjMap.get(tgt.getKey()).get(src.getKey());

					if (lhs != rhs) {
						res.held = false;
					}
				}));

		return res.held;
	}

	/**
	 * Set the weight of an edge
	 * 
	 * @param src
	 *            The source node of the edge
	 * @param tgt
	 *            The target node of the edge
	 * @param weight
	 *            The weight of the edge
	 */
	public void setWeight(T src, T tgt, int weight) {
		adjMap.get(src).put(tgt, weight);
	}

	/**
	 * Convert this to a different graph representation
	 * 
	 * @return The new representation of this graph
	 */
	public Graph<T> toGraph() {
		Graph<T> ret = new Graph<>();

		adjMap.entrySet()
				.forEach(src -> src.getValue().entrySet()
						.forEach(tgt -> ret.addEdge(src.getKey(),
								tgt.getKey(), tgt.getValue())));

		return ret;
	}

	/**
	 * Convert an adjacency map back into a stream
	 * 
	 * @param os
	 *            The stream to convert to
	 */
	public void toStream(OutputStream os) {
		PrintStream ps = new PrintStream(os);

		adjMap.entrySet().forEach(src -> {
			src.getValue().entrySet()
					.forEach(tgt -> ps.printf("%d ", tgt.getValue()));
			ps.println();
		});
		ps.close();
	}
}