covers: ○ Graph formal definition ○ Types of graphs ○ Graph representation methods ○ Application of graphs to solve real world problems Introduction to Graphs
● A graph is a mathematical structure for representing
relationships consisting of a set of nodes (or vertices) connected by edges (or arcs/lines) ● Graphs are ubiquitous in computer science because they provide a handy way to represent a relationship between pairs of objects Introduction to Graphs (II) ● The objects represent items of interest such as programs, people, cities, or web pages, and we place an edge between a pair of nodes if they are related in a certain way. ● For example, an edge between a pair of people might indicate that they like (or, in alternate scenarios, that they don’t like) each other Introduction to Graphs (III) ● An edge between a pair of courses might indicate that one needs to be taken before the other ● Two nodes in a graph can be referred to as adjacent if there's an edge between them ● Two nodes in a graph can be referred to as connected if there's a path between them ● A path is a series of one or more nodes where consecutive nodes are adjacent Introduction to Graphs (IV) ● A path is a cycle if it starts and ends in the same node ● A simple cycle is one that contains at least three nodes and repeats only the first and last nodes Introduction to Graphs (V) Figure 1: A graph with 4 nodes and 6 edges ● Directed/digraphs , provides a way to represent how things are connected together and how to get from one thing to another by following the path ● On the other hand, there is no directions on the path in undirected graph Introduction to Graphs (VI)
● Formally, a directed graph , G , consists of a
nonempty set, V(G) , called the vertices of G , and a set, E(G) , called the edges of G ● An element of V(G) is called a vertex , and an element of E(G) is called edge Introduction to Graphs (VII) ● Graph, G, is composed of underlying set for the vertices, V, and set of edges, E. ● Based on the graph shown; ○ V = { 1, 2, 3, 4, 5, 6 } ○ E = { { 6, 4 } , { 4 ,5 }, { 4, 3 } , { 3, 2 }, { 5, 2 } , { 2, 1 } {5,1} } Common Types of Graphs • Directed Graphs - paths/edges have directions. Order of vertices and edges matter • Undirected Graphs - paths/edges have no directions. Order of vertices and edges doesn’t matter. • Cyclic Graphs - directed graph with at least one cycle • Directed Acyclic Graphs (DAG) - directed graph without cycles Common Types of Graphs (II) • Disconnected Graphs - Vertices in a graph do not need to be connected to other vertices • Vertex Labelled Graph - each vertex is labeled with some data in addition to the data that identifies the vertex • Edge Labelled Graph - Edge labeled graph is a graph where the edges are associated with labels Common Types of Graphs (III) • Weighted Graphs - A weighted graph is an edge labeled graph where the labels can be operated on by the usual arithmetic and logical operators Graph Representation: Adjacency Matrix Representation
● There are many ways to represent a graph
● We have already seen two ways: you can draw it, or you can represent it with sets —as in G = (V, E) ● Another common representation is with an adjacency matrix Graph Representation: Adjacency Matrix Representation
● For a graph with V, vertices, an adjacency matrix is
a ∣V∣ × ∣V∣ matrix of 0’s and 1’s, where the entry in row i and column j is 1 if and only if the edge ( i, j ) exist in the graph Graph Representation: Adjacency Matrix Representation