-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFriendGraph.java
More file actions
117 lines (80 loc) · 2.74 KB
/
Copy pathFriendGraph.java
File metadata and controls
117 lines (80 loc) · 2.74 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
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
package PamakBook;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import javax.swing.*;
import edu.uci.ics.jung.algorithms.layout.CircleLayout;
import edu.uci.ics.jung.algorithms.shortestpath.UnweightedShortestPath;
import edu.uci.ics.jung.graph.*;
import edu.uci.ics.jung.visualization.*;
import edu.uci.ics.jung.visualization.decorators.ToStringLabeller;
@SuppressWarnings("serial")
public class FriendGraph extends JFrame{
private Graph <String, String> friendGraph;
private JLabel diameterLabel;
private JPanel LabelPanel;
private Container contentPane;
private BorderLayout layout;
@SuppressWarnings("unchecked")
FriendGraph(ArrayList<User> users)
{
contentPane = new Container();
layout = new BorderLayout();
LabelPanel = new JPanel();
contentPane.setLayout(layout);
friendGraph = new SparseMultigraph<>();
for(User u : users)
{
friendGraph.addVertex(u.getName());
}
for(User u : users)
{
for(User f: u.getFriends())
{
if(u.isFriend(f) && !friendGraph.containsEdge(friendGraph.findEdge(f.getName(), u.getName())))
{
friendGraph.addEdge(u.getName() +" to " + f.getName(), u.getName(), f.getName());
}
}
}
VisualizationViewer<Integer, Paint> vv = new VisualizationViewer<Integer, Paint>(new CircleLayout(friendGraph));
vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<>());
diameterLabel = new JLabel();
diameterLabel.setText("Diameter = "+ findDiameter(friendGraph));
diameterLabel.setSize(11, 11);
LabelPanel.add(diameterLabel);
contentPane.add(vv, BorderLayout.CENTER);
contentPane.add(LabelPanel, BorderLayout.SOUTH);
this.setVisible(true);
this.setContentPane(contentPane);
this.setSize(800, 1000);
this.setLocation(750, 500);
this.setTitle("Friend Graph");
}
public <V, E> double findDiameter(Graph<V, E> friendGraph)
{
double diameter = 0;
if(friendGraph == null || friendGraph.getVertexCount() == 0) //if there is no diagram of diagram has no vertices
{
diameter = 0;
}
Collection<V> vertices= friendGraph.getVertices();
//value to find shortest paths between vertices
UnweightedShortestPath<V, E> shortestPath = new UnweightedShortestPath<V, E>(friendGraph);
for(V source : vertices)
{
Map<V, Number> distances = shortestPath.getDistanceMap(source); //to store all shortest paths together
for(Number dist: distances.values())
{
if (dist!= null)
{
diameter = Math.max(diameter, dist.floatValue()); //finds the maximum shortest path between the shotests paths
}
}
}
return diameter;
}
}
//Artemis Dara-2025