You are on page 1of 17

Data Structures

Graphs
 one of the most versatile structures used in
computer programming
 have shape dictated by a physical problem
 nodes are called vertices (the singular is vertex)

Graphs

* Property of STI
Page 1 of 17

Data Structures

Graphs

 circles represent freeway interchanges and


straight lines connecting the circles represent
freeways segments
 the circles are the vertices and the lines are the
edges

Graphs

* Property of STI
Page 2 of 17

Data Structures

Graphs
 Adjacency
if they are connected by a single edge
 Neighbors
 Path
 a sequence of edges
 Connected Graphs


Graphs

* Property of STI
Page 3 of 17

Data Structures

Graphs
 Directed and Weighted Graphs


Non-directed graphs - edges dont have a


direction
Directed graphs - model situations in which
can go in only one direction along an edge
Weight - a number that can represent the
physical distance between two vertices

 Historical Information

Graphs

* Property of STI
Page 4 of 17

Data Structures

Graphs
 Vertices

class Vertex
{
public char label;
public boolean wasVisited;
public Vertex(char lab)
{
label = lab;
wasVisited = false;
}
}

Graphs

* Property of STI
Page 5 of 17

Data Structures

Graphs
 Adjacency Matrix


Graphs

two-dimensional array in which the


elements indicate whether an edge is
present between two vertices

* Property of STI
Page 6 of 17

Data Structures

Graphs
 Adjacency List


Graphs

refers to a linked list of a kind examined in


the discussion of recursion

* Property of STI
Page 7 of 17

Data Structures

Graphs
 Adding Vertices and Edges to a Graph


Creation of vertex

vertexList[nVerts++] = new Vertex(F);




Inserting the edge


addjMat[1][3] = 1;
addjMat[3][1] = 1;

Graphs

* Property of STI
Page 8 of 17

Data Structures

Graphs
class Graph
{
private final int MAX_VERTS = 20;
private Vertex vertexList[];
private int adjMat[][];
private int nVerts;
public Graph()
{
vertexList = new Vertex[MAX_VERTS];
adjMat = new int[MAX_VERTS][MAX_VERTS];
nVerts = 0;
for(int j=0; j<MAX_VERTS; j++)
for(int k=0; k<MAX_VERTS; k++)
adjMat[j][k] = 0;
theQueue = new Queue();
}
public void addVertex(char lab)
{
vertexList[nVerts++] = new Vertex(lab);
}
public void addEdge(int start, int end)
{
adjMat[start][end] = 1;
adjMat[end][start] = 1;
}
public void displayVertex(int v)
{
System.out.print(vertexList[v].label);
}
}
Graphs

* Property of STI
Page 9 of 17

Data Structures

Searches
 Depth-First Search

Graphs

Rule 1: If possible, visit an adjacent


unvisited vertex, mark it, and push it on the
stack.
Rule 2: If you cant follow Rule 1, then, if
possible, pop vertex off the stack.
Rule 3: If you cant follow Rule 1 and Rule
2, its finished.

* Property of STI
Page 10 of 17

Data Structures

Searches
 Depth-First Search

public int getAdjUnvisitedVertex(int v)


{
for(int j=0; j<nVerts; j++)
if(adjMat[v][j]==1 &&
vertexList[j].wasVisited==false)
return j;
return -1;
} \
Graphs

* Property of STI
Page 11 of 17

Data Structures

Searches
 Depth-First Search
public void dfs()
{
vertexList[0].wasVisited = true;
displayVertex(0);
theStack.push(0);
while( !theStack.isEmpty() )
{
int v = getAdjUnvisitedVertex
( theStack.peek() );
if(v == -1)
theStack.pop();
else
{
vertexList[v].wasVisited = true;
displayVertex(v);
theStack.push(v);
}
}
for(int j=0; j<nVerts; j++)
vertexList[j].wasVisited = false;
}
Graphs

* Property of STI
Page 12 of 17

Data Structures

Searches
 Depth-First Search creating a graph object
Graph theGraph = new Graph();
theGraph.addVertex('A');
theGraph.addVertex('B');
theGraph.addVertex('C');
theGraph.addVertex('D');
theGraph.addVertex('E');
theGraph.addEdge(0, 1);
theGraph.addEdge(1, 2);
theGraph.addEdge(0, 3);
theGraph.addEdge(3, 4);
System.out.print("Visits: ");
theGraph.dfs();
System.out.println();

Graphs

* Property of STI
Page 13 of 17

Data Structures

Searches
 Breadth-First Search - stay as close as
possible to the starting point

Graphs

Rule 1: Visit the next unvisited vertex (if


there is one) thats adjacent to the current
vertex, mark it, and insert it into the queue.
Rule 2: If you cant carry out Rule 1
because there are no more unvisited
vertices, remove a vertex from the queue (if
possible) and make it the current vertex.
Rule 3: If you cant carry out Rule 2
because the queue is empty, its finished.

* Property of STI
Page 14 of 17

Data Structures

Searches
 Breadth-First Search

Graphs

* Property of STI
Page 15 of 17

Data Structures

Searches
 Breadth-First Search
public void bfs()
{
vertexList[0].wasVisited = true;
displayVertex(0);
theQueue.insert(0);
int v2;
while( !theQueue.isEmpty() )
{
int v1 = theQueue.remove();
while((v2=getAdjUnvisitedVertex(v1)) != -1)
{
vertexList[v2].wasVisited = true;
displayVertex(v2);
theQueue.insert(v2);
}
}
for(int j=0; j<nVerts; j++)
vertexList[j].wasVisited = false;
}

Graphs

* Property of STI
Page 16 of 17

Data Structures

Minimum Spanning Trees

while( !theStack.isEmpty() )
{
int currentVertex = theStack.peek();
int v =getAdjUnvisitedVertex(currentVertex);
if(v == -1)
theStack.pop();
else
{
vertexList[v].wasVisited = true;
theStack.push(v);
displayVertex(currentVertex);
displayVertex(v);
System.out.print(" ");
}
}
for(int j=0; j<nVerts; j++)
vertexList[j].wasVisited = false;
}
Graphs

* Property of STI
Page 17 of 17