Class SimilarItemsGraph
java.lang.Object
org.troy.capstone.data_structures.SimilarItemsGraph
Graph data structure to represent similar items. Uses an adjacency list representation.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classInner class to represent an edge in the graph. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected List<SimilarItemsGraph.Edge>[]Adjacency list to represent edges between items.private final ItemRepoRepository to store items.private static final floatMinimum similarity score required for two items to be considered similar and have an edge between them in the graph.private static final intThe number of similar items to display for selected items.protected intNumber of items in the graph.private final TableThe original table containing the items, used for retrieving items by index. -
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor.SimilarItemsGraph(ItemRepo itemRepo, Table table, Boolean buildGraph) Constructor forSimilarItemsGraph. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidaddEdge(int sourceItemIndex, int destItemIndex, float weight) Adds an undirected edge between two items in the graph with the given weight.private booleanDetermines whether the graph can be built based on the providedbuildGraphvalue or the configuration.private List<SimilarItemsGraph.Edge> dijkstra(int startIndex) Basic algorithm taken from [2].private voidFills the graph with edges based on the similarity scores between items in theItemRepo.findSimilarItems(String itemId) Finds similar items for the given item ID.
-
Field Details
-
MIN_SIMILARITY_SCORE
private static final float MIN_SIMILARITY_SCOREMinimum similarity score required for two items to be considered similar and have an edge between them in the graph. Set to the 95th percentile of all similarity scores.- See Also:
-
NUM_SIMILAR_ITEMS_TO_DISPLAY
private static final int NUM_SIMILAR_ITEMS_TO_DISPLAYThe number of similar items to display for selected items.- See Also:
-
numItems
protected int numItemsNumber of items in the graph. -
itemRepo
Repository to store items. Iterated over when filling the graph. -
table
The original table containing the items, used for retrieving items by index. -
adjacencyList
Adjacency list to represent edges between items.
-
-
Constructor Details
-
SimilarItemsGraph
public SimilarItemsGraph()Default constructor. Only used to allow for subclassing in theSimilarItemsGraphTestclass. All fields are initialized to null. -
SimilarItemsGraph
Constructor forSimilarItemsGraph. Initializes the graph with the givenItemRepo.- Parameters:
itemRepo- TheItemRepocontaining the items to be added to the graph.table- The originalTablecontaining the items, used for retrieving items by index when filling the graph.buildGraph- ABooleanvalue indicating whether to build the graph. If null, the value from the configuration will be used.
-
-
Method Details
-
findSimilarItems
Finds similar items for the given item ID.- Parameters:
itemId- The ID of the item for which to find similar items.- Returns:
- A list of
VBoxpanels representing the similar items. - Throws:
RuntimeException- if the item is not found in the hash map.
-
canBuild
Determines whether the graph can be built based on the providedbuildGraphvalue or the configuration.- Parameters:
buildGraph- ThebuildGraphvalue to check. If null, the configuration value is used.- Returns:
- true if the graph can be built, false otherwise.
-
addEdge
private void addEdge(int sourceItemIndex, int destItemIndex, float weight) Adds an undirected edge between two items in the graph with the given weight.- Parameters:
sourceItemIndex- The index of the source item.destItemIndex- The index of the destination item.weight- The weight of the edge, representing the similarity score between the two items.- Preconditions:
- sourceItemIndex and destItemIndex should be valid item indices corresponding to items in the graph. weight should be a non-negative float representing the similarity score between the two items.
- Postconditions:
- An undirected edge is added between the source and destination items in the graph with the specified weight. The adjacency list is updated to reflect the new edge. Nothing is done if the weight is below the minimum similarity score threshold.
-
fillGraph
private void fillGraph()Fills the graph with edges based on the similarity scores between items in theItemRepo.- Preconditions:
itemReposhould be properly initialized and contain valid items with a defined similarity method to calculate similarity scores between items. The number of items in theitemReposhould match the expected number of items in the graph.- Postconditions:
- The graph is filled with undirected edges between items based on their similarity scores
-
dijkstra
Basic algorithm taken from [2]. Computes the shortest paths from the start index to all other items in the graph.- Parameters:
startIndex- The index of the starting item from which to compute the shortest paths to all other items in the graph.- Returns:
- A list of
Edgeobjects, where each object contains the index of a similar item and its corresponding similarity score. The list is sorted in descending order of similarity scores and limited to the number of similar items to display. - Preconditions:
- startIndex should be a valid item index corresponding to an item in the graph. The graph should be properly initialized with edges representing the similarity scores between items.
- Postconditions:
- The method returns a list of
Edgeobjects, where each object contains the index of a similar item and its corresponding similarity score. The list is sorted in descending order of similarity scores and limited to the number of similar items to display. Items that are not reachable from the start index (i.e., have a distance ofFloat.MAX_VALUE) are excluded from the returned list.
-