Presentation is loading. Please wait.

Presentation is loading. Please wait.

Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an.

Similar presentations


Presentation on theme: "Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an."— Presentation transcript:

1 Graphs Chapter 8 from Drozdek

2 Definitions 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.

3 More definitions A path from v 1 to v n is a sequence of edges from v 1 to v n. If none of the edges are repeated and v 1 = v n 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 K n if for each pair of distinct vertices there is exactly one edge connecting them.

4 Representations 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 A ij = 1 if there exists an edge(v i,v j ) A ij = 0 otherwise Incidence matrix Is a binary matrix such that each entry of the matrix is A ij = 1 if edge e j is incident with vertex v i A ij = 0 otherwise

5 Traversals 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.

6 Depth 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

7 Breadth 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

8 Shortest 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?

9 Label 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.

10 genericShortestPathAlgorithm(weightedSimple digraph, vertex first) for all vertices v currDist(v) = ∞ currDist(first) = 0 initialize toBeChecked while toBeChecked is not empty v = a vertex from toBeChecked remove v from toBeChecked for all vertices u adjacent to v if currDist(u) > currDist(v) + weight(edge(vu)) currDist(u) = currDist(v) + weight(edge(vu)) predecessor(u) = v add u toBeChecked if it is not there Generic Shortest Path

11 Differences 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)

12 Example

13 Label Correcting What happens if a label has a negative weight? The label setting methods fail. So the label correcting methods solve this issue.

14 Ford’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))

15 Example


Download ppt "Graphs Chapter 8 from Drozdek. Definitions A graph is a generalization of a tree. A simple graph consists of a nonempty set of vertices and possibly an."

Similar presentations


Ads by Google