0% found this document useful (0 votes)
5 views

Ch8_Graphs

The document provides an overview of graph theory concepts, including definitions of graphs, types of graphs such as trees and spanning trees, and algorithms for finding minimal-weight spanning trees like Prim's and Kruskal's algorithms. It also discusses graph traversal methods, including depth-first and breadth-first traversal, as well as the concept of independent sets and graph coloring. Key topics include planar graphs, chromatic numbers, and the historical context of Euler's work on the Konigsberg Bridge Problem.

Uploaded by

2491178958
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Ch8_Graphs

The document provides an overview of graph theory concepts, including definitions of graphs, types of graphs such as trees and spanning trees, and algorithms for finding minimal-weight spanning trees like Prim's and Kruskal's algorithms. It also discusses graph traversal methods, including depth-first and breadth-first traversal, as well as the concept of independent sets and graph coloring. Key topics include planar graphs, chromatic numbers, and the historical context of Euler's work on the Konigsberg Bridge Problem.

Uploaded by

2491178958
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Graphs

Graphs
• Vertex or Node
• Edge
• Naming Nodes
• Adjacent Nodes or Neighbors
• Degree of a Node
• Multigraph
Complete Graphs
• Clique - fully connected set of nodes
Chromatic Number
• n-colorable - Colorable using n colors.
• Are the following graph 3-colorable?
• 2-colorable?

• The minimum number of colors needed to color a


graph with no adjacent nodes being the same color
is called the Chromatic Number of the graph
Graph Coloring
Planar
• A graph is planar if it can be drawn so that no
edges cross.
• Is the following planar?
Planar and Colorable
• Every planar graph is 4-colorable. (Proof?)
• All maps are planar. (They have to be.)
• Is every planar graph 4-colorable?
– For over 100 years this was merely a conjecture
because nobody could prove it.
– In 1976, however, Appel and Haken used a computer to
do a proof by exhaustive checking. Their proof contains
over 1900 cases!
– Other graph theorists are not pleased with this proof
because it is hard to verify for oneself, so there is still
on ongoing search for a "human" proof that other
mathematicians can verify.
– Nobody has ever found a counterexample to the
theorem, so we do believe that the theorem is true.
– Find a nice proof and you will become instantly famous
among mathematicians!
Walk
• A walk is a sequence of edges denoted by
pairs of vertices since edges don't have
names.
• A walk can visit a vertex more than once.
• A walk can use an edge more than once.
Walk
• One possible walk in the graph below is A, B,
D, E, B, D. {{A,B},{B,D}, {D,E}, {E,B}, {B,D}}
Trail and Path
• A trail is a walk in which no edge is used more
than once.

• A path is a trail in which no vertex is visited


more than once, unless it forms a cycle.

• A cycle is a path in which the first vertex is the


same as the last vertex.
Walk
• Walk – A B D C A B E
• Trail – A B D E B
• Path – A B D E
• Cycle – A B E D C A
• Length – Number of edges in the walk, trail, or
path.
Euler Trail
• Euler was a famous mathematician that lived from
1707 to 1783. The people of Konigsberg once posed
a problem to Euler that has become known as the
Konigsberg Bridge Problem. A river flows through
the town of Konigsberg. There are two islands in
the river with bridges connecting the islands to
each other and to the shores of the river. The
people of Konigsberg enjoyed walking through their
town and were curious whether there was a way to
take a walk that used each bridge exactly once.
Konigsberg Bridge Problem

Is there a trail which uses every edge


exactly once?
Konigsberg
• It takes two edges to visit a vertex that is not
at the beginning or end---one edge to arrive
and one to leave. A vertex that has 2j (an even
number) of edges can be visited j times.
• Euler trail exists in any graph in which every
vertex has even degree.
• If there are two vertices of odd degree and no
more, then a Euler trail exists, but it starts at
one of the odd vertices and ends at the other.
Connected
• A graph is connected if there is a path
between any two vertices of the graph.
• A graph is disconnected is there are vertices
for which there is no path. (Looks like two or
more graphs.)
Cyclic
• A graph is cyclic if it contains a cycle.
• A graph is acyclic if it does not contain a cycle.
Tree
• A tree is a connected, acyclic graph.
• A leaf is a vertex in a tree with a degree of 1.
Rooted Tree
• Has a designated root vertex.
• Typically drawn top down or bottom up.
• Height - # edges from root to farthest leaf.
• Child
• Parent
• Ancestor
• Descendant
• Ordered
Binary Search Tree
• Each vertex has at most two
children.
• Ordered - Left child or right
child has meaning.
• For every vertex v
– All values stored in the left
subtree are less than the value
of v.
– All values stored in the right
subtree are greater than the
value stored in v.
Ordered Traversal
• Start at root and
proceed around
outer edge.
– CCW low to high
– CW high to low
• When you pass
below a node,
write it's value.
Building a binary search tree
• 84, 35, 20, 49, 150, 112, 120, 37, 3, 155
• Start at the root.
• If this node is empty put the value there.
• If this node is not empty and the value is greater
than this nodes value move to right child.
• If this node is not empty and the value is less
than this nodes value move to left child.
• Repeat until proper place is found.
Spanning Tree
• A spanning tree of a connected graph
– Is a sub-graph of the connected graph.
– Is a tree (connected, no cycles)
– Contains ALL of the vertices of the graph.
– There can be many different spanning trees of a
single graph.
Spanning Tree Example
• The graph on the left has the following
spanning trees. Are there more?
Minimal-Weight Spanning Tree
• Weighted Graphs have
weights associated with the
edges.
• Example:
– Vertices are cities.
– Edges represent a pipeline
which needs to be built.
– What is the cheapest path to
connect all cities?
Prim’s Algorithm
• Draw the vertices without edges
• Select a starting vertex
• Find the edge with the minimum weight from the
starting vertex on the original graph and draw it in.
• Find the edge leading from the connected vertices
with the minimum weight, that does not cause a
cycle, and draw it in.
• Repeat the previous step until all vertices are
include
Prim's Algorithm
• Input: A non-empty connected weighted graph with
vertices V and edges E (the weights can be negative).
• Initialize: Vnew = {x}, where x is an arbitrary node (starting
point) from V, Enew = {}
• Repeat until Vnew = V:
– Choose an edge {u, v} with minimal weight such that u is in
Vnew and v is not (if there are multiple edges with the same
weight, any of them may be picked)
– Add v to Vnew, and {u, v} to Enew
• Output: Vnew and Enew describe a minimal spanning tree
Create a minimal-weight spanning tree using
Prim’s algorithm Starting at node A
Create a minimal-weight spanning tree using
Prim’s algorithm Starting at node A
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A}
• E ={}
• Find possible edges where one
vertex is in V and one vertex is
in VNEW.
{A,B} (4)
{A,C} (2)
{A,D} (3)
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C}
• E ={{A,C}}
• Put the edge into E and the
Vertex into VNEW
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C}
• E ={{A,C}}
• Find possible edges where one
vertex is in V and one vertex is
in VNEW.
{A,B} (4) {C,F} (2)
{A,D} (3)
{C,E} (3)
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C,F}
• E ={{A,C},{C,F}}
• Put the edge into E and the
Vertex into VNEW
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C,F}
• E ={{A,C},{C,F}}
• Find possible edges where one
vertex is in V and one vertex is
in VNEW.
{A,B} (4) {F,D} (1)
{A,D} (3) {F,G} (3)
{C,E} (3) {F, E} (4)
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C,F,D}
• E ={{A,C},{C,F},{F,D}}
• Put the edge into E and the
Vertex into VNEW
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C,F,D}
• E ={{A,C},{C,F},{F,D}}
• Find possible edges where one
vertex is in V and one vertex is
in VNEW.
{A,B} (4) {F, E} (4)
{C,E} (3) {D,B} (2)
{F,G} (3) {D,G} (4)
Create a minimal-weight spanning tree
using Prim’s algorithm Starting at node A
• V = {A,B,C,D,E,F,G}
• VNEW = {A,C,F,D,E,B,E,G}
• E ={{A,C},{C,F},{F,D},{D,B}
{C,E},{F,G},{A,B}}
• Repeat until done.
Kruskal’s algorithm
• Pick the edge with the smallest weight and
draw it. If more than one edge has the
smallest weight, pick one and draw it.
• Pick the edge with the smallest weight that
does not cause a cycle and draw it.
• Repeat the above step until all vertices have
been connected.
Create a minimal-weight spanning tree using
Kruskal’s algorithm
Graph Traversal
• Start at some vertex (v) and visit each vertex
that can be reached from v.
• In real applications, visiting a vertex means
performing some operation on it.
• Sometimes, backing up will be required.
Backing up does not count as revisiting a
node.
Depth First Traversal
• Walk from vertex v until a leaf is found.
• Visit all the vertices we come to along the way.
• Back up and take an edge we haven't yet
taken. If no unused edge is found. Back up
and repeat.
Depth First Traversal
• Walk to a leaf, visit along the
way.
– Visit E, visit I, visit A, visit F
• Back up and take the next
available path
– Back up to A then visit D
• Back up and take the next
available path
– Back up to A, no path
– Back up to I, no path
– Back up to E, visit G
Depth First Traversal
• Visited E, I, A, F, D, G
• Visit G, visit B
• Back up and take the next
available path
– Back up to G, visit J, visit H
– Back up to J, visit C
• One depth first traversal
therefore is:
E, I, A, F, D, G, B, J, H, C
Order Preference
• The last example prefers left node over
right node. That means that when a
decision between two nodes must be
made, always choose the left node first.
• I could say to make the visit order
decision alphabetically, that means
when a decision between two nodes is
to be make, always use the one closest
to the front of the alphabet first.
• An alphabetical ordering starting at e
will be the following:
E, G, B, J, C, H, I, A, D, F
Breadth First Traversal
• In a breadth-first traversal we visit all the
vertices that are 1 edge away from our
starting point v
• Next visit all the vertices that are 2 edges
away from v.
• Next visit all the vertices that are 3 edges
away from v.
• Repeat until all vertices have been visited.
Breadth First Traversal
• Start somewhere, visit E
• Visit all vertices that are one edge away
from E.
– Visit I, visit G
• Visit all vertices that are two edges away
from E .
– Visit A, visit B, visit J
• Visit all vertices that are three edges away
from D.
– Visit F, visit D, visit H, visit C
• If Left vertex is preferred over right vertex
the order is: E, I, G, A, B, J, F, D, H, C
• An alphabetical preference would be the
following order: E, G, I, A, B, J, C, D, F, H
Independent Sets of Vertices
• A set of vertices are
independent if none
of the vertices in the
set are adjacent to
any other vertex in
the set.
• {A,E,J}
• {B,H,D}
Back to the coloring
• Consider the graph
coloring problem.

• We want no adjacent
colors.

• How to determine in
an algorithmic way?
Coloring with Independent Sets
• Determine the
Maximal Independent
set. (Not maximum)
{A,B,C,D,E,F,G,H,I,J}
• A is in the set.
• Is B?
• Is C?
• D? E? F? G? H? I? J?
Maximal Independent Set
• {A,B,C,D,E,F,G,H,I,J}
• A is in the set.
• Is B?
• Is C?
• D? F? G? H? I? J?
• {A,D,G,H,I} Maximal
Independent Set
• All of these could be
painted a single color.
Maximal Independent Set
• Unused: {B,C,E,F,J}
• B is in the set?
• Is C? E? F? J?
Maximal Independent Set
• Unused: {B,C,E,F,J}
• B is in the set?
• Is C? E? F? J?
• {B, E, J}
• All of these could be
painted a second
color.
Maximal Independent Set
• Unused {D,F,}
• D is in the set.
• Is F?
• {D, F}
• All of these could be
painted a third color.
Maximal Independent Set
• A different set of choices
could have resulted in a
different coloring.
• This procedure does not
ensure minimal coloring.
• {A,F,G,H,I}
• {B,D, J}
• {C}
• {E}
Maximal Independent Set
• {A,D,G,H,I}
• Maximal means no
unused node could be
added.
• With the choices we
have made, this is as big
as this set can get.
• Maximal does NOT mean
this is the biggest
possible set.

You might also like