Professional Documents
Culture Documents
Assessed ExerciseWeek5 v2 F
Assessed ExerciseWeek5 v2 F
Assessed ExerciseWeek5 v2 F
Task A
Before attempting the task please read the “hints” you can find below.
Implement all methods up to the method public void AddEdge(T from, T to)
(included). We will use two classes, GraphNode and Graph.
Use the completed classes to implement an application (with a GUI) which
allows:
a) to insert a node into a graph (given the ID which you should assume to
be a int)
b) to insert a directed edge between two nodes (given their IDs)
c) to display true only if node m is adjacent to node n (the IDs of node m
and node n are given as input)
d) to display the total number of nodes present in the graph
Open a new project (Windows Forms Application for C#) called Graph
// constructor
public GraphNode(T id)
{
this.id = id;
adjList = new LinkedList<T>();
}
if (n.ID.CompareTo(node.ID) == 0)
return true;
// add a new node (with this “id”) to the list of nodes of the graph
public void AddNode(T id)
{
// to be completed
}
// Add a directed edge between the node with id "from" and the node with id
“to”
public void AddEdge(T from, T to)
{
// to be completed
//Hint: Find the node with id “from” in the list of nodes and then
//use the GraphNode method to add an edge to the node with id “to”
}
} //end class
Hints to complete the task: Create a Windows Forms Application for C# and add the
classes and methods implemented above. Many of the methods that need to be
completed have been discussed (and coded) in the lab, so please check the slides
on Moodle (Lab A) when completing the classes (you can also find there some test
codes that show how to call and use the methods). In the created Windows Forms
Application you can use textboxes to read the inputs. We have seen how to add and
use a data structure inside a Windows Forms Application in Lab B of Week 1.
The user interface should be a GUI (use a Windows Forms application for C#). In
your screencast, test the application on the graph shown below in Figure 1 (below).
Hints to complete the task: We cover graph traversals in the lectures so please
consider also the slides on Moodle when completing this task. To implement the
application you will need to use and extend the classes Graph and GraphNode (see
slides of the lecture and lab). For the point c) use the idea of graph traversal
(implement as either DFS or BFS, see the starting code below, that you need to add
in the class Graph).
//Perform a DFS traversal starting at the node with id “startID”
//leaving a list of visited id’s in the visited list.
toVisit.Push(startID);
while (toVisit.Count != 0)
{
//to be completed. Hint: get current node to the list of visited nodes
and add its adjacent nodes (only those not already visited) to toVist
}
}
toVisit.Enqueue(startID);
while (toVisit.Count != 0)
{
//to be completed. Hint: get current node to the list of visited nodes
and add its adjacent nodes (only those not already visited) to toVisit
}
}
Ayub
Dayna
Reilly
Haniy
a
Sidney
Pegg
y
Coe
n
Josef Tracey
f
Amelia
Figure 1
The graph shows a social network with 10 people and the established direct edges.
A direct edge between person “A” and person “B” denotes that “A” is friend of “B”. If
there is a direct edge going from from A to B (but not from B to A) then “A is friend of
B” but “B is not friend of A” (friendship can be one-sided in this social network).
For point c) of the application, display the names of all the persons that can be
reached in the graph starting from a person provided in input. For example, in the
graph above, the application should display that the persons that can be reached
starting from Haniya are: Tracey, Joseff, Sidney, Coen. Notice that this is a very
useful function used in online social networks to discover the “friends of friends”.
Week5_TaskA
…
…
…
…
…
…
…
…
…
*
* Explain the key parts of the code
???key parts of the code?
???Explanation?
*?
*?
*
* how does the solution links with algorithms and data structures concepts, theory /methodology
*?
*?
*
* explain how confident you are that your solution works
* and successfully solves the task
* (e.g., a presentation of the code execution on an example)
* ?Demonstration of the code working
*?
*/
Week5_TaskB
…
…
…
…
…
…
…
…
…
*
* Explain the key parts of the code
???key parts of the code?
???Explanation?
*?
*?
*
* how does the solution links with algorithms and data structures concepts, theory /methodology
*?
*?
*
* explain how confident you are that your solution works
* and successfully solves the task
* (e.g., a presentation of the code execution on an example)
* ?Demonstration of the code working
*?
*/