# Dynamic Programming - Graphs

• Graphs

### Graphs: Basic Terminology

• Graph: Set of vertices and set of edges
• Vertices also called nodes

• Edges connect two vertices (possibly the same)

• Represent vertices with circles and edges with lines between circles

• Digraph: graph whose edges are directed (represented with arrows)

• Examples: cities and roads, courses and prerequisites

• Weighted graph: edges have a weight = cost of traversing

### Graphs: Terminology on Paths

• Path: sequence of vertices in which edges connect successive

• Cycle: a path that starts and ends on the same vertex

• Cyclic and acyclic graphs: have or don't have any cycles

• Simple path: doesn't cross itself
• Simple paths can't contain cycles

• Length of a path: Number of edges or sum of edge weights

### Graph Representation

• Adjacency Matrix: 2D array shows weight
• 2D array
• Row for each vertex
• Column for each vertex
• Entries contain weight of edge from row vertex to column vertex
• Entries contain $\infty$ if no edge from row vertex to column vertex
• Entries contain 0 on diagonal

### All Pairs Shortest Paths

• We will consider path lengths first, then finding the path