Reading 35
Graphs
Graphs are non-linear data structures containing nodes, connected by edges.
- Vertex - ‘node’, the data object
- Edge - connection between vertices
- Neighbor - adjacent node
- Degree - number of edges connected to a vertex
Directed vs Undirected
Undirected
A graph where each edge is uni-directional or bi-directional. This means the edges do not POINT toward a particular direction.

Directed
Also called a Digraph, a directed graph contains edges which all point to a particular direction or node.

Complete, Connected, Disconnected
Complete - all nodes are connected with each other.

Connected - all nodes have at least one edge.

Disconnected - Some nodes may not be connected.

Cyclic vs Acyclic
Cyclic graphs can be traversed and arrive back where they started. Acyclic graphs have no cycles.
Adjacency Matrix
A two dimensional array that represents the connections between nodes. 0 means no connections. 1 represents a present connection.

Traversal
Breadth First
- Enqueue the start node
- Create while loop (while node: )
- Dequeue first node
- If dequeued node has unvisited children, add children to visited and insert into queue
ALGORITHM BreadthFirst(vertex)
DECLARE nodes <-- new List()
DECLARE breadth <-- new Queue()
DECLARE visited <-- new Set()
breadth.Enqueue(vertex)
visited.Add(vertex)
while (breadth is not empty)
DECLARE front <-- breadth.Dequeue()
nodes.Add(front)
for each child in front.Children
if(child is not visited)
visited.Add(child)
breadth.Enqueue(child)
return nodes;
Depth First
- Push root into stack
- While stack not empty:
- Peek stack
- If top node has unvisited children, mark top as visited and push unvisited children back into stack
- If top has 0 unvisited children, Pop stack
- Repeat until empty
source: https://codefellows.github.io/common_curriculum/data_structures_and_algorithms/Code_401/class-35/resources/graphs.html