Data Structures and Algorithms
Data Structures and Algorithms
Data Structures and Algorithms
ALGORITHMS
Graphs and Applications
What is a graph?
• A graph is a mathematical structure that represents
relationships among entities in the real world.
3
Graph Animation
https://liveexample.pearsoncmg.com/dsanimation/GraphLearningTooleBook.html
4
Basic Graph Terminologies
What is a graph? G=(V, E)
Define a graph
Directed vs. undirected graphs
Weighted vs. unweighted graphs
Adjacent vertices
Incident (Two edges sharing the same common point)
Degree (The number of edges of a point)
Neighbor
Loop ( a loop is an edge that connects a vertex to itself)
5
Directed vs Undirected Graph
Peter Jane
Cindy Mark
Wendy
6
Basic Graph Terminologies
• Parallel edge: If two vertices are connected by two or more Edges
• Simple graph : is one that doesn’t have any loops or parallel edges.
• Complete graph: every two pairs of vertices are connected
• Spanning tree of a graph G is a connected subgraph of G, and the
subgraph is a tree that contains all vertices in G.
A D
A D
B E
B E
7
Representing Graphs
Representing Vertices
Representing Edges: Edge Array
Representing Edges: Edge Objects
Representing Edges: Adjacency Matrices
Representing Edges: Adjacency Lists
8
Representing Vertices
String[] vertices = {“Seattle“, “San Francisco“, “Los Angles”,
“Denver”, “Kansas City”, “Chicago”, … };
City[] vertices = {city0, city1, … };
List<String> vertices;
9
Representing Edges: Edge Array
int[][] edges = {{0, 1}, {0, 3} {0, 5}, {1, 0}, {1, 2}, … };
10
Representing Edges: Edge Object
public class Edge {
int u, v;
public Edge(int u, int v) {
this.u = u;
this.v = v;
}
Atlanta neighbors[8] 4 7 9 10 11
Miami neighbors[9] 8 11
Dallas neighbors[10] 2 4 8 11
Houston neighbors[11] 8 9 10
14
«interface» The generic type V is the type for vertices.
Graph<V>
+getSize(): int Returns the number of vertices in the graph.
+getVertices(): List<V> Returns the vertices in the graph.
+getVertex(index: int): V Returns the vertex object for the specified vertex index.
+getIndex(v: V): int Returns the index for the specified vertex.
+getNeighbors(index: int): List<Integer> Returns the neighbors of vertex with the specified index.
+getDegree(index: int): int Returns the degree for a specified vertex index.
+printEdges(): void
Prints the edges.
+clear(): void Clears the graph.
+addVertex(v: V): boolean Returns true if v is added to the graph. Returns false if v
is already in the graph.
+addEdge(u: int, v: int): boolean Adds an edge from u to v to the graph throws
IllegalArgumentException if u or v is invalid. Returns
true if the edge is added and false if (u, v) is already in Graph
the graph.
Adds an edge into the adjacency edge list.
+addEdge(e: Edge): boolean
+remove(v: V): boolean Removes a vertex from the graph.
+remove(u: int, v: int): boolean Removes an edge from the graph.
+dfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a depth-first search tree starting from v.
+bfs(v: int): UnWeightedGraph<V>.SearchTree Obtains a breadth-first search tree starting from v.
.
UnweightedGraph<V>
#vertices: List<V>
#neighbors: List<List<Edge>>
Vertices in the graph.
Neighbors for each vertex in the graph.
UnweightedGraph
+UnweightedGraph() Constructs an empty graph.
+UnweightedGraph(vertices: V[], edges: Constructs a graph with the specified edges and vertices
int[][]) stored in arrays.
+UnweightedGraph(vertices: List<V>, Constructs a graph with the specified edges and vertices
edges: List<Edge>)
stored in lists.
+UnweightedGraph(edges: int[][], Constructs a graph with the specified edges in an array
and the integer vertices 1, 2, ....
TestGraph
numberOfVertices: int)
+UnweightedGraph(edges: List<Edge>, Constructs a graph with the specified edges in a list and
numberOfVertices: int) the integer vertices 1, 2, ….
15
Graph Example
Graph Example
Graph Example
Output:
The number of vertices in graph1: 12
The vertex with index 1 is San Francisco
The index for Miami is 9
The edges for graph1:
Seattle (0): (Seattle, San Francisco) (Seattle, Denver) (Seattle, Chicago)
San Francisco (1): (San Francisco, Seattle) (San Francisco, Los Angeles) (San Francisco, Denver)
Los Angeles (2): (Los Angeles, San Francisco) (Los Angeles, Denver) (Los Angeles, Kansas City) (Los Angeles, Dallas)
Denver (3): (Denver, Seattle) (Denver, San Francisco) (Denver, Los Angeles) (Denver, Kansas City) (Denver, Chicago)
Kansas City (4): (Kansas City, Los Angeles) (Kansas City, Denver) (Kansas City, Chicago) (Kansas City, New York) (Kansas City, Atlanta) (Kansas City, Dallas)
Chicago (5): (Chicago, Seattle) (Chicago, Denver) (Chicago, Kansas City) (Chicago, Boston) (Chicago, New York)
Boston (6): (Boston, Chicago) (Boston, New York)
New York (7): (New York, Kansas City) (New York, Chicago) (New York, Boston) (New York, Atlanta)
Atlanta (8): (Atlanta, Kansas City) (Atlanta, New York) (Atlanta, Miami) (Atlanta, Dallas) (Atlanta, Houston)
Miami (9): (Miami, Atlanta) (Miami, Houston)
Dallas (10): (Dallas, Los Angeles) (Dallas, Kansas City) (Dallas, Atlanta) (Dallas, Houston)
Houston (11): (Houston, Atlanta) (Houston, Miami) (Houston, Dallas)
Graph Example
Graph Example
Output:
The number of vertices in graph2: 5
The edges for graph2:
Peter (0): (Peter, Mark)
Jane (1): (Jane, Mark)
Mark (2): (Mark, Wendy)
Cindy (3): (Cindy, Wendy)
Wendy (4):
Applications of the Graph
`
Detecting whether there is a path between two vertices.
21
References
1. Introduction to Java Programming and Data Structures,
comprehensive version 11th Edition, Y Daniel Liang, 2018