0% found this document useful (0 votes)
28 views25 pages

Graphs

A graph is a data structure consisting of vertices and edges connecting the vertices. Graphs can model relationships between objects. They have many applications including modeling computer networks, maps, and hypertext links. Graphs can be undirected or directed depending on whether the edges have directionality. Common graph terminology includes adjacent vertices, paths, complete graphs, and weighted graphs. Graphs can be implemented using an adjacency matrix or linked list representation.

Uploaded by

l226207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views25 pages

Graphs

A graph is a data structure consisting of vertices and edges connecting the vertices. Graphs can model relationships between objects. They have many applications including modeling computer networks, maps, and hypertext links. Graphs can be undirected or directed depending on whether the edges have directionality. Common graph terminology includes adjacent vertices, paths, complete graphs, and weighted graphs. Graphs can be implemented using an adjacency matrix or linked list representation.

Uploaded by

l226207
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Graphs

What is a graph?
• A data structure that consists of a set of nodes
(vertices) and a set of edges between the vertices.
• The set of edges describes relationships among the
vertices.

1 2

3 4
Applications

Computer networks
Maps

Hypertext Circuits
Formal definition of graphs
• A graph G is defined as follows:
G=(V,E)
V: a finite, nonempty set of vertices
E: a set of edges (pairs of vertices)
Undirected graphs
• When the edges in a graph have no
direction, the graph is called undirected
undirected graph

The order of vertices in E


is not important for
undirected graphs!!
Directed graphs
• When the edges in a graph have a direction,
the graph is called directed.

The order of vertices in E


is important for
directed graphs!!

E(Graph2) = {(1,3) (3,1) (5,9) (9,11) (5,7)


Trees vs graphs
• Trees are special cases of graphs!!

I
Graph terminology
• Adjacent nodes: two nodes are adjacent if
they are connected by an edge

7 is adjacent from 5
5 7 or
5 is adjacent to 7

7 is adjacent from/to 5
5 7 or
5 is adjacent from/to 7
Graph terminology
• Path: a sequence of vertices that connect
two nodes in a graph.

1 2

e.g., a path from 1 to 4:


<1, 2, 3, 4>

3 4
Graph terminology
• Complete graph: a graph in which every
vertex is directly connected to every other
vertex
Graph terminology (cont.)
• What is the number of edges E in a
complete directed graph with V vertices?

E=V * (V-1)

or E=O(V2)
Graph terminology (cont.)
• What is the number of edges E in a
complete undirected graph with V vertices?

E=V* (V-1) / 2

or E=O(V2)
Graph terminology (cont.)
• Weighted graph: a graph in which each edge
carries a value
Graph Implementation
• Array-based
• Linked-list-based
Array-based implementation
• Use a 1D array to represent the vertices
• Use a 2D array (i.e., adjacency matrix) to
represent the edges
Array-based implementation (cont’d)
Array-Based Implementation (cont.)
• Memory required
– O(V+V2)=O(V2)
• Preferred when
– The graph is dense: E = O(V2)
• Advantage
– Can quickly determine
if there is an edge between two vertices

• Disadvantage
– No quick way to determine the vertices adjacent
from another vertex
Linked-list-based implementation
• Use a 1D array to represent the vertices
• Use a list for each vertex v which contains the
vertices which are adjacent from v (adjacency
list)
Linked-list-based implementation (cont’d)
Link-List-based Implementation (cont.)
• Memory required
– O(V + E)

• Disadvantage
– No quick way to determine whether
there is an edge between vertices u and v

• Advantage
– Can quickly determine the
vertices adjacent from a given vertex
Graph specification based on
adjacency matrix representation
const int NULL_EDGE = 0;
private:
template<class VertexType> int numVertices;
class GraphType { int maxVertices;
public: VertexType* vertices;
GraphType(int); int **edges;
~GraphType();
void MakeEmpty(); };
bool IsEmpty() const;
bool IsFull() const;
void AddVertex(VertexType);
void AddEdge(VertexType, VertexType, int);
int WeightIs(VertexType, VertexType);
void GetToVertices(VertexType, QueType<VertexType>&);
void ClearMarks();
void MarkVertex(VertexType);
bool IsMarked(VertexType) const;
template<class VertexType>
GraphType<VertexType>::GraphType(int maxV)
{
numVertices = 0;
maxVertices = maxV;

vertices = new VertexType[maxV];

edges = new int[maxV];


for(int i = 0; i < maxV; i++)
edges[i] = new int[maxV];
}
template<class VertexType>
GraphType<VertexType>::~GraphType()
{
delete [] vertices;

for(int i = 0; i < maxVertices; i++)


delete [] edges[i];
delete [] edges;

}
void GraphType<VertexType>::AddVertex(VertexType
vertex)
{
vertices[numVertices] = vertex;

for(int index = 0; index < numVertices; index++) {


edges[numVertices][index] = NULL_EDGE;
edges[index][numVertices] = NULL_EDGE;
}

numVertices++;
}
template<class VertexType>
void GraphType<VertexType>::AddEdge(VertexType
fromVertex, VertexType toVertex, int weight)
{
int row;
int column;

row = IndexIs(vertices, fromVertex);


col = IndexIs(vertices, toVertex);
edges[row][col] = weight;
}

You might also like