In what concerns the continuous evaluation solving exercises grade during the semester, you should submit until 23:59 of December 14th
(this exercise will still be available for submission after that deadline, but without couting towards your grade)
[to understand the context of this problem, you should read the class #09 exercise sheet]


[AED049] Node Degree

In this problem you should only submit a graph.h file with a self-containing definition of a class Graph.

Base Code

Use as a basis the class Graph (see code | download code) which implements a graph (that can have directions and weights) using an adjacency list, assumes nodes are identified by integers between 1 and |V| and already has methods for adding an edge, reading a graph and doing a basic DFS and a basic BFS.

The Problem

Add a new method int outDegree(int v) to the class. The function should return the outgoing degree of the node v, that is, the number of connections that have v as start node (if the graph is undirected, than the outgoing degree is the same as the degree).

All the input graphs will be simple (no self-loops or parallel edges) and unweighted, but they can directed or undirected. It is guaranteed that |V|<=100 and |E|<=500.

Submission

You should submit the file graph.h with the method outDegree added to Graph as requested (and without removing any of the other existing methods). You can however create additional methods and/or attribute variables, if you need them.

Mooshak will use the following code to link to your class, read the inputs, build the graphs and call your method, printing its result.

#include <iostream>
#include <string>

#include "graph.h"

int main() {

  // Read one input graph
  Graph *g = Graph::readGraph();

  // read q integer indicating nodes to ask for outDegree
  int q;
  std::cin >> q;
  for (int i=0; i<q; i++) {
    int x;
    std::cin >> x;
    std::cout << "g->outDegree(" << x << ") = " << g->outDegree(x) << std::endl;    
  }
  
  delete g;
  
  return 0;
}

Example Input 1 Example Output 1
9 undirected unweighted
10
1 2
1 3
2 4
3 4
4 5
5 6
5 7
6 9
6 7
7 8
4
1 4 9 6
g->outDegree(1) = 2
g->outDegree(4) = 3
g->outDegree(9) = 1
g->outDegree(6) = 3

Explanation of Example Input 1

The Graph* g of the example input 1 corresponds to the following figure:


Example Input 2 Example Output 2
8 directed unweighted
8
3 1
1 2
2 4
5 4
5 6
6 7
6 8
8 7
3
1 4 6
g->outDegree(1) = 1
g->outDegree(4) = 0
g->outDegree(6) = 2

Explanation of Example Input 2

The Graph* g of the example input 2 corresponds to the following figure:



Algorithms and Data Structures (L.EIC011) 2024/2025
DCC/FCUP & DEI/FEUP - University of Porto