1 of 9

Lecture 14: Graphs, networkx, and Sets

Autumn 2026

1

Sli.do

#cse163

Questions?

Icebreaker

Favorite Halloween or horror movie?

CSE 163

CSE 163: Graphs, Networkx, Sets

​

​

2 of 9

Announcements

  • Resubmission Form 2 due Sunday, November 1 at 11:59pm
    • Fill out the resubmission form AND re-submit code on Gradescope
    • Homework 1 and Homework 2 eligible (pick one!)
    • Last chance to re-submit Homework 1
    • No form, no regrade
  • Programming Practice 4 due Sunday, November 1 at 11:59pm on Gradescope
  • Homework 4 due Thursday, November 12 at 11:59pm on Gradescope
  • Project Part 2 (EDA) due Sunday, November 15 at 11:59pm on Gradescope

2

Questions?

CSE 163: Graphs, Networkx, Sets

​

​

3 of 9

Inheritance

  • Often multiple classes share common attributes and behaviors
    • There are different types of courses at a university: lecture courses (CSE 163!), lab courses, seminars, capstone courses
    • All share basic attributes: course code, credit hours, instructor
    • Each type of course can have a unique component that distinguishes it from the others
  • Inheritance is an OOP concept that allows us to create a new class (subclass or child class) that gets (or inherits) attributes and methods from an existing class (superclass or parent class)
    • A subclass can inherit from multiple superclasses, but be careful of method calls that share the same name but have different behaviors.

3

CSE 163: Graphs, Networkx, Sets

​

​

4 of 9

Inheritance Example

4

class Dog:

  def __init__(self, name: str) -> None:

    self._name: str = name

�

  def bark(self) -> None:

    print(self._name + ': Woof')

class ServiceDog(Dog):

  def __init__(self, name: str, service: str) -> None:

super().__init__(name)

​

    self._service: str = service

�

  def bark(self) -> None:

    print(self._name + ‘: *alert bark*')

CSE 163: Graphs, Networkx, Sets

​

​

5 of 9

Graphs

  • Nodes and Edges
    • Nodes represent some data in the graph
    • Edges represent relationships between the data
  • Suppose the graph on the right represents a “social network”
    • B’s friends: A, D, E
    • C’s friends: A, D
    • B + C’s mutuals: A, D
  • What else can graphs represent?

5

CSE 163: Graphs, Networkx, Sets

​

​

6 of 9

Sets

  • A collection of values, without duplicates or order!
    • Consider when it may be more useful to use a set versus a list
  • Constructing sets from a list and strings
    • odd = set([1, 3, 5]) → {1, 3, 5}
    • even = set([2, 4, 4, 6, 8, 8, 8]) → {2, 4, 6, 8}
    • name = set("Adrian") → {'i', 'A', 'd', 'n', 'r', 'a'}

6

CSE 163: Graphs, Networkx, Sets

​

​

7 of 9

Set Operations

7

Mathematical Operation

In Python

Example

Membership

in

4 in prime → False

Union

|

odd | prime → {1, 2, 3, 5}

Intersection

&

odd & prime → {3, 5}

Difference

-

odd - prime → {1}

Symmetric difference

^

odd ^ prime → {1, 2}

odd = {1, 3, 5}

prime = {2, 3, 5}

CSE 163: Graphs, Networkx, Sets

​

​

8 of 9

Set Methods

  • Add an element
    • set.add(element)
    • set = set | {element} # Add using set union

​

  • Remove an element
    • set.remove(element) # Raises an error if element not found
    • set.discard(element) # No error if element not found
    • set = set - {element} # Removal using set difference

​

  • Remove and return an arbitrary element
    • set.pop()

8

CSE 163: Graphs, Networkx, Sets

​

​

9 of 9

Homework 4 Tips!

  • Start early!
  • graphs.py
    • Edges and nodes are in a list or convenience
  • analysis.py
    • Read through the starter code thoroughly
    • Consider what the different data structures represent
    • The two recommendation algorithms you will write are fairly similar!

9

CSE 163: Graphs, Networkx, Sets

​

​