Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph...

15
Graphs Graphs Chapter 8 from Drozdek

Transcript of Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph...

GraphsGraphs

Chapter 8 from Drozdek

DefinitionsDefinitions

• A graph is a generalization of a tree.• A simple graph consists of a nonempty set

of vertices and possibly an empty set of edges• Each edge being a set of two vertices.

• A directed graph, or digraph, where the edges are directed from one vertice to another.

More definitionsMore definitions

• A path from v1 to vn is a sequence of edges from v1 to vn.

• If none of the edges are repeated and v1 = vn then this is a circuit.

• A weighted graph if each edge has an assigned number.

• A graph with n vertices is complete and is denoted Kn if for each pair of distinct vertices there is exactly one edge connecting them.

RepresentationsRepresentations

• Adjacency list• Specifies all the vertices that are adjacent to each vertex

in the graph.• Adjacency matrix

• Is a binary matrix such that each entry of the matrix is• Aij = 1 if there exists an edge(vi,vj)• Aij = 0 otherwise

• Incidence matrix• Is a binary matrix such that each entry of the matrix is• Aij = 1 if edge ej is incident with vertex vi

• Aij = 0 otherwise

TraversalsTraversals

• Depth first search• Each vertex v is visited and then each unvisited

vertex adjacent to v is visited. • If a vertex has no adjacent vertices or all of its

adjacent vertices have been visited, we backtrack to the predecessor of v.

• The traversal ends when the initial vertices is returned to.

Depth First SearchDepth First Search

• DFS(v)• Num(v) = i++;• For all vertices u adjacent to v

• If num(u) is 0• Attach edge(uv) to edges• DFS(u)

• depthFirstSearch()• For all vertices v

• Num(v) = 0;• Edges = null;• i=1• While there is a vertex v such that num(v) is 0

• DFS(v)• Output edges

Breadth First SearchBreadth First Search• breadthFirstSearch()

• For all vertices u• Num(u) = 0

• Edges = null• i=1• While there is a vertex v such that num(v) == 0• Num(v)=i++• Enqueue(v)• While queue is not empty

• V = dequeue()• For all vertices u adjacent to v

• If num(u) is 0• Num(u) = i++• Enqueue(u)• Attach edge(vu) to edges

• Output edges

Shortest PathShortest Path

• Classic problem in graph theory.• A path between two vertices on a weighted

graph is needed.• What is the shortest path to get there?

Label Setting and Label CorrectingLabel Setting and Label Correcting

• There are two main categories for shortest path problems.

• Label setting• For each pass, one vertex is set to a constant value until

the end of execution.

• This limits to positive values.

• Label correcting• Allows for the value of any vertex to be changed at any

time.

genericShortestPathAlgorithm(weightedSimple digraph, vertex first)for all vertices v

currDist(v) = ∞currDist(first) = 0initialize toBeCheckedwhile toBeChecked is not empty

v = a vertex from toBeCheckedremove v from toBeCheckedfor all vertices u adjacent to vif currDist(u) > currDist(v) +

weight(edge(vu))currDist(u) = currDist(v) +

weight(edge(vu))predecessor(u) = vadd u toBeChecked if it is not there

Generic Shortest PathGeneric Shortest Path

DifferencesDifferences

• How is v = a vertex in toBeChecked chosen?

• Dijkstra chose a vertex with the smallest current distance.

• To obtain Dijkstra’s algorithm we change the selection of v to:• v = a vertex in toBeChecked with mimimal

currDist(v)

ExampleExample

Label CorrectingLabel Correcting

• What happens if a label has a negative weight?

• The label setting methods fail.• So the label correcting methods solve this

issue.

Ford’s AlgorithmFord’s Algorithm

FordAlgorthim(weight simple diagraph, vertex first)for all vertices v

currDist(v) = ∞while there is an edge(vu)

such that currDist(u) >currDist(v) + weight(edge(vu))

currDist(u) = currDist(v) + weight(edge(vu))

ExampleExample