Assignment 3: Graphs and Friend Circles
Learning Goals
- Analyze new graph problems and propose solutions.
- Implement a graph search algorithm.
- Compare graph representations.
- Trace through graph algorithms.
Grading
This assignment will be graded as pass/no-pass by a TA. To pass the assignment, you must
- Complete every question in the PDF and submit your answers to gradescope.
- Complete the programming component and submit your answers to gradescope.
- Meet with your TA during your learning community sessions. I prefer both partners to be present during the walk-through, but you can each meet with the TA separately if needed.
- Walk the TA through your answers. Do not expect to make corrections during the walk-through.
- The TA will then either
- mark your assignment as 100% on gradescope, or
- inform you that you have some corrections to make.
- If corrections are needed, then you will need to complete them and conduct a new walk-through with a TA.
If you have concerns about the grading walk-through, you can meet with me after you’ve first met with a TA.
Overview
This assignment includes two parts:
- Answer the question in this PDF.
- Write a program for the problem described in section Friend Circles Problem (must be compatible with Python 3.8).
You may work on this assignment alone or with a partner (I much prefer you to work with a partner). Please send me a message on Slack if you want me to assign you to work with a random partner. Whether you work alone or with a partner, only one partner will submit a both parts to gradescope.
Three key points:
- you should either print the PDF and write your answers directly on the printed sheet, or add annotations to the PDF with, for example, Edge (Windows) or Preview (OSX) (using the correct format and spacing will help us grade),
- ensure that the uploading partner adds the other partners after submission, and
- if you have any questions please ask them on Slack.
Example Execution
I always like to start by showing you the end result.
If you do not see an animation above this line (or if you see the animation but you don’t see the progress bar), you will need to refresh the page (sometimes more than once). Or you can go directly to the player.
You can copy and past from the above animation!
Friend Circles Problem
There are \(n\) students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature, i.e., if A
is a friend of B
and B
is a friend of C
, then A
is also a friend of C
.
A friend circle is a group of students who are directly or indirectly friends. Indirect friendships can result from multiple levels of indirection.
Your program will take a filename as a program argument, and it will print the number of friend circles found in the class data found in the file.
The input file contains an \(n\) by \(n\) adjacency matrix containing the characters Y
and N
. A Y
found in the \(i^{th}\) row and \(j^{th}\) column indicates that \(student_i\) and \(student_j\) are friends; an N
indicates they are not friends.
You can assume the following: - Each element of input file will be Y
or N
- The number of rows and columns in the input file will be equal - All students are friends with themselves (the diagonal of the matrix is filled with Y
s) - If \(student_i\) is friends with \(student_j\), then \(student_j\) is friends with \(student_i\)
Generating Test Cases
If you are interested in testing your code on more inputs, here is the script that I use to generate test input files (you can download the script here):
##!/usr/bin/env python3
"""
Generating a new test file:
./generate_friend_matrix.py 8 0.32 > test_file.txt
"""
from argparse import ArgumentParser
from random import random
= ArgumentParser("Generate an adjacency matrix.")
arg_parser "-n", help="Number of students.", default=10, type=int)
arg_parser.add_argument(
arg_parser.add_argument("-p", help="Probability of two students being friends", default=0.2, type=float
)
= arg_parser.parse_args()
args
= [["N" for _ in range(args.n)] for _ in range(args.n)]
friends for i in range(args.n):
for j in range(i, args.n):
if i == j or random() < args.p:
= "Y"
friends[i][j] = "Y"
friends[j][i]
print("\n".join(["".join(f) for f in friends]))
Reading the input file
Here is my Python3 code for reading the input file:
if __name__ == "__main__":
= ArgumentParser("Count the number of friend circles.")
arg_parser "file", help="Path to an adjacency matrix file.")
arg_parser.add_argument(
= arg_parser.parse_args()
args
with open(args.file, "r") as matrix_file:
= [line.strip() for line in matrix_file.readlines()]
friends_matrix
= count_friend_circles(friends_matrix)
num_circles print(f"Number of friend circles: {num_circles}")
Submitting Your Assignment
You will submit your PDF assignment on gradescope. Only one partner should submit your PDF. The submitter will add the other partner through the gradescope interface.
You will also submit your code to gradescope. Your file must be named friend_circles.py
. Only one partner should submit your code. The submitter will add the other partner through the gradescope interface. Please add your names in comments at the top of the file.
To pass the autograder, your output must exactly match the expected output. Your program output should be similar to the example execution above, and the autograder on gradescope will show you the correct output if yours is not formatted properly. You can use text-compare to compare your output to the expected output and that should give you an idea if you have a misspelled word or extra space (or if I do).
I’ve enabled a leaderboard for the assignment. You do not need to worry about it affecting your grade. I just thought it looked like a cute feature. Feel free to give yourself (your group) any name you’d like for the leaderboard.
Additional details for using gradescope can be found here: