Assessed ExerciseWeek5 v2 F

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

1

Assessed Exercise Week 5


The exercise has 2 assessed tasks: Task A and Task B

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

Add two new generic classes GraphNode and Graph.


Starting codes for Generic Class: GraphNode
public class GraphNode<T>
{
private T id; // data stored in the node (“id” of the node).
private LinkedList<T> adjList; // adjacency list of the node
//Use LinkedList from C#

// constructor
public GraphNode(T id)
{
this.id = id;
adjList = new LinkedList<T>();
}

// set and get the data stored in the node


public T ID
{
// to be completed
}

//add a directed edge from “this” node to the node "to”


public void AddEdge(GraphNode<T> to)
{
//to be completed.
//hint: add the id of node “to” to the adjacency list
}

// returns the adjacent list of the node


public LinkedList<T> GetAdjList()
{
//to be completed.
// hint: returns the adjacency list
}
}

6G5Z1002 Algorithms and Data Structures ©Manchester Metropolitan University


Starting codes for Generic Class: Graph
public class Graph<T> where T : IComparable
{

// list of all the nodes in the graph. Use LinkedList from C#


private LinkedList<GraphNode<T>> nodes;

// constructor – set nodes to new empty list


public Graph()
{
nodes = new LinkedList<GraphNode<T>>();
}

// only returns true if the graph’s list of nodes is empty


public bool IsEmptyGraph()
{
// to be completed
}

// returns the total number of nodes present in the graph


public int NumNodesGraph()
{
// to be completed

// only returns true if node is present in the graph


public bool ContainsGraph(GraphNode<T> node)
{
// to be completed. Hint: Search through the full list of nodes (search
of the node is based on the id)

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

//returns the node with this id


public GraphNode<T> GetNodeByID(T id)
{
// to be completed
//Hint: Search through the list of nodes for a node with this id
}

// return true if “to” is adjacent to “from”

public bool IsAdjacent(GraphNode<T> from, GraphNode<T> to)


{
// 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.

Task B Social Network App


Implement an application in C# (with a GUI) which manages a social network using a
graph (see Figure 1 below). The application should allow the user to:

a) Insert / Remove a person in the graph by providing its name as input.


b) Insert a direct edge between 2 persons in the graph given their names.
c) Display the names of all the persons that can be reached in the graph by starting
from a person given as input (see example in Figure 1 below); highlight the input
provided (eg changing the text colour in the textbox) in case all the persons
present in the graph can be reached starting from the person given as input.

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.

public void DepthFirstTraverse(T startID, ref List<T> visited)


{
LinkedList<T> adj;
Stack<T> toVisit = new Stack<T>();

GraphNode<T> current = new GraphNode<T>(startID);

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

}
}

//Perform a BFS traversal starting at the node with id “startID”


//leaving a list of visited id’s in the visited list.

public void BreadthFirstTraverse(T startID, ref List<T> visited)


{
LinkedList<T> adj;
Queue<T> toVisit = new Queue<T>();
GraphNode<T> current = new GraphNode<T>(startID);

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 code organization for the task:




* what methods were used,









* what are the class members and class hierarchy





*
* Explain the key parts of the code
???key parts of the code?

???Explanation?

* Explain the programming techniques used to solve the task:


?programming techniques??
???

*?
*?
*

* Explain the logic behind your proposed solution


?code logic? 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 code organization for the task:




* what methods were used,









* what are the class members and class hierarchy





*
* Explain the key parts of the code
???key parts of the code?

???Explanation?

* Explain the programming techniques used to solve the task:


?programming techniques??
???

*?
*?
*

* Explain the logic behind your proposed solution


?code logic? 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
*?
*/

You might also like