Lab 7
Due: 04/29
Searching and Sorting
Objectives
- 20 Points - Searching for friends.
Description
Searching for friends.
In this task we are desining classes for searching for persons within a social network (think Facebook). The social network should be represented as a undirected graph consisting of nodes (the people that participate in the social network) and undirected edges that representing friendship relationships. There is an edge from node to node if the persons and are friends. Create classes in package lecture.lab7
to store such a social network. There should be a main class SocialNetwork
representing the full network. How you design classes for Persons and edges of the network to is up to you. For each person in the network you need to store their name, a nickname that identifies them within the social network, their age, and gender. We call these the characteristics of a person. The class SocialNetwork
should expose a method public boolean search(Condition e)
which return true
if the social network matches the condition.
Search conditions
Syntax
Conditions check for existance of nodes and edges with certain characteristics. It is probably best to organize all expression types in a class hierarchy. Expressions are defined recursively as follows:
A term is one of the following:
- Variable: A variable is a term of type node
- Charateristics: If is a variable and is a charaterstic (
name
,nickname
,age
, orgender
), then is a value term of a type depending on the characteristic (int
for age andString
for all other charateristics) - Literal: A literal string or integer
A condition is one of the following:
- Terms: Any term is an expression with the same type of term
- Comparisons: If and are value terms of the same type, then is an expression of type boolean
- Connectivity: If and are variables, then is an expression of type boolean
- Logical Connectives: If and are expressions of type boolean then , , and are expressions of type boolean
For example, a pattern that returns true if there exists a person that is 50
years old can be written as
We use for an expression to the set of all variables appearing in expression .
Semantics
Given a network and a search condition of type boolean, the result of the condition depends on the existance of a successful valuation of the condition. A valuation assigns the variables of the condition () to nodes in the network. A valuation is successful if it evaluates to true
according to the following rules:
- Variable term evaluates to the node assigned to the variable by the valuation:
- Charateristic term evaluate to the value has in characteristic
- Literal term evaluates to itself
- Comparion evaluate to
true
if and evaluate to the same value - Connectivity evaluates to
true
if there exists an edge in the network between nodes and - Logical connectives follow the standard rules of boolean logic
Note that edges in the network are undirected!
Evaluating Search Conditions
To make progress with the implementation it is probably best to start with designing classes to model expressions. Once you have a solid model for such expressions, you need to think about how to evaluate such expressions over a graph. It may be good idea to split the problem into two pieces:
- A component that generates valuations, i.e., assignments of nodes to the variables of a condition
- A component that takes a valuation and evaluates whether the condition is successful for this valuation
Start designing a brute force algorithm that tries out all possible assignments of nodes to variables. Then think about whether there better strategies then just trying all combinations.
Test case
As a test case create the graph shown below for the following persons:
Name | Nickname | Age | Gender |
---|---|---|---|
Peter | Petey | 50 | male |
Alice | AA43 | 23 | female |
Bob | theguy | 44 | male |
Ann | efeffe | 33 | female |
Evaluate the following conditions:
- - which should return
true
because Peter is 50 years old - - which returns
true
, because Ann has a friend (edges are undirected).